你可能会寻找这样的事情:
var customObject = function(){
this.title = "";
}
var setupMain = function(){ //"This" will point to instance, such as co
this.title = "initial setup value";
}
var co = new customObject();
co.setup = setupMain; //Reference to function
co.setup(); //Call the setup function
window.alert(co.title);
另外,如果你不希望有保留每段时间设定setup
函数来创建一个实例,你可以将它原型:
customObject.prototype.setup = setupMain; //Now, every customObject has a setup function
var co = new customObject();
co.setup();
window.alert(co.title);
最后,如果你不想有打电话setup();
每一次,你可以使用const内调用setup
ructor:
var customObject = function(){
this.setup(); //Call shared setupMain function because it's part of the prototype
}
var setupMain = function(){
this.title = "initial setup value";
}
customObject.prototype.setup = setupMain; //This can be shared across many prototypes
var co = new customObject();
window.alert(co.title);
'co.setup'未定义,因为您调用了函数'setupMain',它没有返回值。 – 2012-02-06 17:06:05
好的,那我该怎么回来才能让它分配函数,它的内容是co.setup?谢谢。 – 2012-02-06 17:10:29
在下面添加我的答案,让我知道如果它不清楚。 – 2012-02-06 17:21:44