我想知道這兩種方法有什麼區別。他們都工作,但我不明白,如果第二種方法可能有不良影響?JavaScript:在哪裏放置原型功能
// A. Putting a prototype method outside the function declaration (what I would normally do)
var Cat = function(){
}
Cat.prototype.eat = function(){
// implementation
}
// B. Putting a prototype method inside the function declaration (it works too but the scoping seems different)
var Cat = function(){
Cat.prototype.eat = function(){
// implementation
}
}
感謝您的所有意見。我現在記得爲什麼我擺在這首先:我找不到從原型函數訪問私有變量的方法。我現在明白你不應該那樣做,因爲私有變量只能從創建它們的範圍訪問。如果您需要訪問原型中的變量,則應該使用'this'關鍵字而不是'var'來聲明公共變量, – ChrisRich 2012-08-07 23:55:57