1.prototype(原型對象)有一個屬性叫作constructor,constructor默認指向prototype(原型對象)所在的構造函數函數
2.constructor屬性是定義在prototype上的,那就意味着能夠被實例對象所繼承this
3.能夠使用hasOwnProperty 方法來驗證一個屬性是本身的仍是繼承過來的spa
4.constructor的做用:
做用一:分辨實例對象到底屬於哪一個構造函數 instanceof()
做用二:能夠從一個實例對象建立另外一個實例對象prototype
var Fun = function(){ console.log('hello'); console.log('world!'); } var fun = new Fun(); console.log(Fun.prototype.constructor === Fun); //true console.log(fun.constructor === Fun); //true console.log(Fun.prototype.hasOwnProperty("constructor")); // true console.log(fun.hasOwnProperty("constructor")); //false console.log(fun instanceof Fun);//true var fun1 = new fun.constructor(); console.log(fun1 instanceof Fun); //true
Fun.prototype.createCopy = function(){ console.log(this); console.log(this.constructor); return new this.constructor(); } // this.constructor指向Fun , this指向調用者自己 var fun2 = new Fun(); console.log(fun2.constructor === Fun.prototype.constructor);//true console.log(fun2.constructor === Fun);//true console.log(fun2.createCopy().__proto__ === Fun.prototype);//true
繼承:code
function Father(){ this.sex = '男'; } function Son(){ this.age = 24; Son.superclass.constructor.call(this); } Son.superclass = new Father(); var son = new Son(); console.log(son.age); console.log(son.sex); console.log(Son.superclass); //Father
注意: 因爲constructor屬性是一種原型對象與構造函數的關聯關係,因此咱們修改原型對象的時候務必要當心對象
function A(){ } function B(){ } var a = new A(); A.prototype = B.prototype; console.log(a.constructor); console.log(a.constructor === A);//true console.log(a.constructor === B);//false console.log(a instanceof A);//false console.log(a instanceof B);//false