面向對象實現方法:基於類,基於原型,基於元類
JS採用基於原型(prototype-based):
讀遍歷:對實例進行寫操作時,會爲實例建立相應成員表;讀操作時,若找不到相應屬性,則遍歷原型鏈直到找到或返回undefined爲止。
新建函數時:
function F(){};
實際發生了:1.爲該函數(F)添加prototype屬性;
2.爲prototype對象添加constructor屬性,並使其指向F;
此時若用構造器創建實例f:
var f=new F();
function A(){};
function B(){};
B.prototype=new A();
var b=new B();
alert(b.constructor===A);
此時訪問b的constructor屬性=>遍歷原型鏈=>找到原型(new A()對象)=>訪問new A()的constructor屬性=>遍歷原型鏈=>找到原型A.prototype=>訪問A.prototype.constructor=>取得A故alert(b.constructor===A)爲true
。。。。。。。。霧。。。。。。。。。。。。。。。。。。。