运行此代码时,首先使用构造函数传入的任何值都将Test(callbacks)
成为始终调用的回调,即使在以后的实例化中也是如此Test
function Test(callbacks) {
if (callbacks) {
if (callbacks.callback) {
this.callback = callbacks.callback;
}
}
this.options.complete = $.proxy(this.options.complete, this);
}
Test.prototype = {
options: {
type: "GET",
complete: function() {
this.callback();
}
},
callback: function() { console.log("OVERRIDE ME"); },
execute: function() {
$.ajax(this.options);
}
};
var eins = {callback: function() {console.log("AAA");}};
var zwei = {callback: function() {console.log("BBB");}};
var A = new Test(eins);
var B = new Test(zwei);
A.execute();
B.execute();
运行这段代码,每次你都会得到输出AAA
。如何function() {console.log("AAA");}
成为原型的常量值?