You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"use strict";function_classCallCheck(instance,Constructor){// 直接调用 this 指向windowif(!(instanceinstanceofConstructor)){thrownewTypeError("Cannot call a class as a function");}}varPerson=functionPerson(name){_classCallCheck(this,Person);this.name=name;};
'use strict';function_classCallCheck(instance,Constructor){if(!(instanceinstanceofConstructor)){thrownewTypeError("Cannot call a class as a function");}}varPerson=functionPerson(name){_classCallCheck(this,Person);this.foo='foo';this.name=name;};Person.bar='bar';
直接赋值(没有static)的变量是一个语法糖,和constructor中的this写法并无差异
静态属性会编译成构造函数的属性
原型方法 + 静态方法 + get set方法
classPerson{constructor(name){this.name=name;}sayHello(){return'hello, I am '+this.name;}staticonlySayHello(){return'hello'}getname(){return'kevin';}setname(newName){console.log('new name 为:'+newName)}}
----------------------------------Babel
'use strict';var_createClass=function(){functiondefineProperties(target,props){for(vari=0;i<props.length;i++){// 取出一个需要注入的方法vardescriptor=props[i];// 默认不可以枚举descriptor.enumerable=descriptor.enumerable||false;// 默认可以配置descriptor.configurable=true;// 如果是属性访问器,则设置writable属性if("value"indescriptor)descriptor.writable=true;// 注入方法Object.defineProperty(target,descriptor.key,descriptor);}}returnfunction(Constructor,protoProps,staticProps){// 向Constructor的原型上 添加方法if(protoProps)defineProperties(Constructor.prototype,protoProps);// 向Constructor 自身上 添加方法if(staticProps)defineProperties(Constructor,staticProps);// 返回Constructor本身returnConstructor;};}();function_classCallCheck(instance,Constructor){if(!(instanceinstanceofConstructor)){thrownewTypeError("Cannot call a class as a function");}}/** * 重写该方法,返回Constructor本身,但向他的原型和Constructor注入方法 */varPerson=function(){functionPerson(name){_classCallCheck(this,Person);this.name=name;}// 向Constructor原型和Constructor分别注入方法_createClass(Person,[{key: 'sayHello',value: functionsayHello(){return'hello, I am '+this.name;}},{key: 'name',get: functionget(){return'kevin';},set: functionset(newName){console.log('new name 为:'+newName);}}],// 静态方法[{key: 'onlySayHello',value: functiononlySayHello(){return'hello';}}]);returnPerson;}();
constructor
----------------------------------Babel
静态属性
----------------------------------Babel
原型方法 + 静态方法 + get set方法
----------------------------------Babel
_createClass
对Object.defineProperties()
进行封装,实现对原型方法和静态方法的注入。_createClass
方法,将设有static标识符的函数放置一个数组(静态方法数组),作为第三个参数进行传递The text was updated successfully, but these errors were encountered: