var mymethod = {
init: function(options){
//initialize
//how to call buildhtml from here;
},
buildhtml: function(){
//build inner html
},
sendrequest: function(){
//make ajax to send request
//can I call hoocrequest from here;
},
hoocrequest: function(){
//hooc ajax return value
}
};
如何从init或其他人调用buildhtml或其他?
答案 0 :(得分:0)
var mymethod = {
init: function(options){
this.buildhtml();
},
buildhtml: function(){
alert('build');
}
};
mymethod.init();
如果您从其他上下文调用init
,则此方法无效。例如:
var fn = mymethod.init;
fn(); // type error
在这种情况下,您需要将上下文绑定到函数引用:
var fn = mymethod.init.bind(mymethod);
fn(); // works now
或者在调用函数时提供上下文:
var fn = mymethod.init;
fn.call(mymethod); // works now