var foo = {
p1: function(){
return this.p2;
},
p2: function(){
console.log('i am foo.p2');
}
};
我試圖做類似上面的例子中的東西,但是我運行到哪裏,當我調用一個問題:的Javascript是有關功能的詳細信息,對象的屬性「這個」
var result = foo.p1();
結果=='undefined'
我對'this'在對象上下文中的工作方式感到困惑。有人能解釋我哪裏錯了嗎?
編輯 更完整的例子:
suite_segments.themis = {
//don't re-run themis initialization script
initialized: false,
/**
* Initializer for themis product. Returns true if initialization
* operations were performed, false if not (most likely because
* the product was already initialized -- not a fresh navigation)
*/
init: function(){
//prevent multiple initializations
if(this.initialized)
return false; //did not initialize
this.initialized = true;
//operations
jQuery('#tabs').tabs();
//init success
return this.themis_destroy;
},
/* ----------------------------------------------------------------------------------
* DESTRUCTORS
* ----------------------------------------------------------------------------------/
/**
* Function to be invoked if user navigates away from 'themis' entirely. Other
* sub-destroy type functions will be invoked if necessary when a user switches
* between parts of themis
*
*/
themis_destroy: function(){
console.log('themis_destructor');
this.initialized = false;
},
/**
* Designed to be overwritten every time a segment of themis is loaded. Will be invoked
* ever time a segment of themis is loaded.
*/
themis_sub_destroy: function(){}
};
發佈您正在嘗試或製作[fiddle]的實際代碼(http://jsfiddle.net) – zzzzBov
'foo.p1()'確實會產生'foo.p2'。發佈更完整的摘要。 – MaxArt
我不知道,男人,你的代碼段實際上在JSFiddle中有效。 'suite_segments.themis.init();'真的返回'suite_segments.themis.themis_destroy'。必須有別的東西。 – MaxArt