2017-09-18 38 views
0

我寫下了我在幾句話中學到的內容,真正理解原型和對象包裝器如何工作。如果我錯了,請糾正我,如果我錯過了其他詳細信息,請告訴我。原型和對象包裝器如何在JavaScript中工作?

JavaScript使用原型找上對象的方法和屬性。例如,如果你創建一個字符串"Hello"這是一種原始的字符串值,並嘗試應用該方法split()它("Hello".split()),字符串值強制轉換爲String對象,所以我們可以應用split()方法。該String對象由String.prototype.constructor創建。創建後,JavaScript將在原型對象中查找split()方法。在應用該方法並返回該值之後,不再需要String對象,因此它被犧牲給垃圾收集之神。

回答

2

你基本上是正確的,但堆棧空間和繼承是兩個不同的問題,並在高層次的腳本語言,像JS垃圾回收是如何工作的可能是前途未卜。確實,單行代碼行的中間值會在該行完成後被銷燬。

此外,String.prototype.constructor,簡直是本身一樣StringString的任何「實例」將收到.__proto__引用,該引用將指向String.prototype,從而創建繼承鏈。如果發現對象屬性未被定義爲對象x本身,則JavaScript自動首先檢查由x.__proto__引用的對象,然後x.__proto__.__proto__,一直到大多數情況下 - Object.prototype

請記住,__proto__被JavaScript解釋器之間不同的方式實現,而不應被手動操作。很高興知道這樣的參考可以解釋原型魔法,但是沒有任何情況下你應該直接改變對象的參考。相反,您應該通過new運算符或Object.create來創建類的實例。在JavaScript中一個完整的超/子關係是這樣的:

function Animal(name, weight){ 
    this.name = name, this.weight = weight; 
    this.alive = true; 
    this.hungry = true; 
} 

// At this point you could put in Animal.prototype = Object.create(Object.prototype); but JavaScript will set it automatically, so it’s unnecessary 

Animal.prototype.kill = function(){ 
    this.alive = false; 
} 

Animal.prototype.feed = function(){ 
    this.hungry = false; 
} 

function Cat(name, weight){ 
    Animal.call(this, name, weight); 
    this.lives = 9; 
} 


// Sometimes people instance a new parent like 
// Cat.prototype = new Animal('doesntmatter', 420); 
// The result is the same except that using Object.create saves the overhead 
// of running a full constructor for an object that doesn’t need it 
Cat.prototype = Object.create(Animal.prototype); 

Cat.prototype.kill = function(){ 
    this.lives--; 
    if(this.lives <= 0) this.alive = false; 
}; 


var a = new Animal(); 
var c = new Cat(); 

/* a.feed and b.feed now reference the same function object, 
but c.kill has been overridden since its prototype `__proto__` chain first 
visits Cat.prototype, then Animal.prototype, then Object.prototype. */ 

最後,ES6引入了class關鍵字是語法糖爲這個系統,並抽象構造函數的init方法。

相關問題