《你不知道的JavaScript2》-对象与类

1、[[Prototype]]机制就是指对象中的一个内部链接引用另一个对象。
2、本质就是:对象之间的关联关系。
3、尽量避免在[[Prototype]]链的不同级别中使用相同的命名,尽量少使用容易被重写的通用方法名,提倡使用更有描述性的方法名,尤其少要写清相应对象行为的类型。
4、委托行为意味着某些对象在找到属性或者方法引用时会把这个请求委托给另一个对象。
5、委托最好在内部实现,不要直接暴露出去。
6、Object.create()的polyfill:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

if (typeof Object.create !== "function") {
    Object.create = function (proto, propertiesObject) {
        if (typeof proto !== 'object' && typeof proto !== 'function') {
            throw new TypeError('Object prototype may only be an Object: ' + proto);
        } else if (proto === null) {
            throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.");
        }

        if (typeof propertiesObject != 'undefined') throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument.");

        function F() {}
        F.prototype = proto;

        return new F();
    };
}

7、函数不是构造函数,但是当且仅当使用new时,函数调用会变成“构造函数调用“;
8、ES6新增语法class,但实际上js中并不存在类。
9、类通过复制操作被实例化为对象形式。
10、类实例是由一个特殊但类方法构造的,这个方法名通常和类名相同,被称为构造函数,这个方法的任务是初始化实例需要的所有信息。

猜你喜欢

转载自blog.csdn.net/wuweitiandian/article/details/79852276