-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6 classes && Object descriptor #15
Comments
classCallCheckexample: function foo(options) {
classCallCheck(this, foo)
} source code: var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
|
possibleConstructorReturnexample: inherits(foo, bar)
function foo(options) {
classCallCheck(this, foo)
var _this = possibleConstructorReturn(this, (foo.__proto__ || Object.getPrototypeOf(foo)).call(this, options));
} source code: var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}; |
createClassexample: function foo(options) {
classCallCheck(this, foo)
}
createClass(foo, [{
key: 'hello',
value: function hello() {
var _this2 = this
}
}, {
key: ...,
value: ...
}]) source code: var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}(); |
Object descriptor在ES6中,由于 Symbol类型的特殊性,用Symbol类型的值来做对象的key与常规的定义或修改不同,而Object.defineProperty 是定义key为Symbol的属性的方法之一。 对象里目前存在的属性描述符有两种主要形式:数据描述符和存取描述符。数据描述符是一个具有值的属性,该值可能是可写的,也可能不是可写的。存取描述符是由getter-setter函数对描述的属性。描述符必须是这两种形式之一;不能同时是两者。 数据描述符和存取描述符均具有以下可选键值:configurable 数据描述符同时具有以下可选键值:value 存取描述符同时具有以下可选键值:get 如果一个描述符不具有value,writable,get 和 set 任意一个关键字,那么它将被认为是一个数据描述符。如果一个描述符同时有(value或writable)和(get或set)关键字,将会产生一个异常。 引用:
|
ReflectReflect是一个仅提供静态方法的全局对象,存在的目的是为了替代一些诸如 Function.prototype.apply() 这种调用方式,并且更为通用。从功能上来看与被替代的方法一致,只有返回的值或别的一些小细节上存在不同。 Reflect.constructor() |
extends(inherits)
source code:
superClass
类型不为function
或不为null
,抛出一个类型错误subClass
的原型设定为通过Object.create
函数创建的原型,参数为superClass.prototype
与constructor options,其中 constructor options 的 value 为 subClass
Object.setPrototypeOf
函数存在,执行它,参数为subClass
跟superClass
,若不存在,直接把subClass
的__proto__
指向superClass
The text was updated successfully, but these errors were encountered: