我想知道这两种方法有什么区别。他们都工作,但我不明白,如果第二种方法可能有不良影响?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