我想知道函数体构造完成后我们还可以更改它吗?
var O = function(someValue){
this.hello = function(){
return "hello, " + someValue;
}
}
O.prototype.hello = function(){
return "hhhhhhh";
}
var i = new O("chris");
i.hello(); // -> this still returns the old definition "hello, chris"
JavaScript 语句 O.prototype.hello = function(){....}
不会覆盖和重新定义 hello 函数的行为。这是为什么 ?我知道如果您尝试重用参数 someValue
,将会出现类型错误。
// this will fail since it can't find the parameter 'someValue'
O.prototype.hello = function(){
return "aloha, " + someValue;
}
我想知道为什么它允许在运行时添加功能,例如
O.prototype.newFunction = function(){
return "this is a new function";
}
i.newFunction(); // print 'this is a new function' with no problem.
但定义一旦定义就不允许更改。
我做错什么了吗 ?我们如何覆盖和重新定义类中的函数?有没有办法重用我们之前传入的参数来创建对象?在这种情况下,如果我们想向其扩展更多功能,我们如何重用 someValue
。
最佳答案
当您使用new
时,构造函数内this
的值指向新创建的对象(有关new
如何工作的更多信息,看看 this answer 和 this answer )。因此,您的新实例 i
有一个 hello
函数。当您尝试访问对象的属性时,它会沿着原型(prototype)链向上移动,直到找到它。由于 hello
存在于对象的实例上,因此无需沿着原型(prototype)链向上访问返回 hhhhhhhh
的 hello
版本。从某种意义上说,您已经覆盖了实例中的默认实现。
如果您不在构造函数内将 hello
分配给 this
,您就会看到此行为:
var O = function(someValue) {
}
O.prototype.hello = function(){
return "hhhhhhh";
}
var i = new O("chris");
console.log(i.hello()); //this prints out hhhhhhh
你所做的有点倒退。原型(prototype)基本上提供了某种东西的“默认”形式,您可以在每个实例的基础上覆盖它。仅当在对象上找不到您要查找的属性时,才会使用默认形式。也就是说,JavaScript 将开始沿着原型(prototype)链向上走,看看是否可以找到与您要查找的属性相匹配的属性。如果它找到了它,它就会使用它。否则,它将返回未定义
。
在第一种情况下,您基本上拥有的内容如下:
Object.prototype.hello (not defined; returns "undefined")
|
+----O.prototype.hello (returns "hhhhhhhh")
|
+----i.hello (returns "hello, chris")
因此,当您执行 i.hello
时,JavaScript 会发现 i
上有一个 hello
属性并使用它。现在,如果您没有显式定义 hello
属性,则基本上具有以下内容:
Object.prototype.hello (not defined; returns "undefined")
|
+----O.prototype.hello (returns "hhhhhhhh")
|
+----i.hello (is "undefined", so JavaScript will walk up the chain until
it sees O.prototype.hello, which does have a defined value
it can use.)
这意味着您可以在原型(prototype)中提供默认实现,然后覆盖它(在某种意义上它就像子类化)。您还可以通过直接修改实例来修改每个实例的行为。原型(prototype)上的 hello
版本是一种故障安全和后备功能。
编辑:您的问题的答案:
基于每个实例进行重写意味着您将属性或函数附加到特定实例。例如,您可以这样做:
i.goodbye = function() {
return "Goodbye, cruel world!";
};
这意味着此行为特定于该特定实例(即,仅针对i
,而不是您可能创建的任何其他实例)。
如果你取出this
,那么你基本上就拥有了:
hello = function() {
return "hello, " + someValue;
}
这相当于做:
window.hello = function() {
return "hello, " + someValue;
}
因此,在本例中,hello
是对该函数的全局引用。这意味着 hello
未附加到您的任何对象。
this.hello = function() { .... };
,则 hello
可能是未定义的。我还讨论了 JavaScript 用来尝试解析对象属性的一般过程。正如我之前提到的,它涉及到原型(prototype)链的上游。
关于Javascript重新定义并覆盖现有函数体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12183011/