1. 程式人生 > >JS-- 一只tom貓 講解構造函數 實例 實例原型 實例原型的實例原型

JS-- 一只tom貓 講解構造函數 實例 實例原型 實例原型的實例原型

.proto str ref tps class 參考 obj prot iss

參考地址:https://github.com/mqyqingfeng/Blog/issues/2 感謝這位大佬
下面說說我的理解:

第一,看下人物: tom--一只叫tom的貓 Cat()--貓的構造函數 cat_model---貓的原型(是只具有代表的貓)
技術分享圖片

第二,看下 tom是怎樣被造出來的,是公貓和母貓嗎?
技術分享圖片

附上代碼:
var Cat(){
this.name=name
}

function objectFactory(){
var obj = new Object()
Constructor = [].shift.call(arguments)
obj.__proto__ = Constructor.prototype

Constructor.apply(obj,arguments)
return typeof ret == ‘object‘ ? ret:obj;
}

辣麽 var tom = new Cat(‘tom‘) 和 var tom = objectFactory(Cat,‘tom‘) 是一樣的

JS-- 一只tom貓 講解構造函數 實例 實例原型 實例原型的實例原型