var foo = {
p1: function(){
return this.p2;
},
p2: function(){
console.log('i am foo.p2');
}
};
上記の例と同様のことをしようとしていますが、次の場合に問題が発生します。
var result = foo.p1();
結果=='未定義'
'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(){}
};