《你不知道的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、類實例是由一個特殊但類方法構造的,這個方法名通常和類名相同,被稱爲構造函數,這個方法的任務是初始化實例需要的所有信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章