次のようなものを探しているかもしれません:
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();
を呼び出す必要がない場合は、コンストラクタ内で setup
を呼び出すことができます。
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);