var obj = {
toString:function(){ return 'obj のスコープ内 (スコープ内)';}, //console.log( の実行を容易にするために toString 関数を書き換えます) this) Output
func: function(){
// ここでの関数はオブジェクト "object" に直接従属します
console.log(this);
var innerFunc = function(){
// nここでの関数は特定のオブジェクトの直接のメンバーではなく、別の関数の単なる変数です
};
innerFunc(); }
}; 🎜>obj.func();
// 「obj のスコープ内 (スコープ内)」を出力
// 「Window の一部の関連コンテンツ」を出力
Missing これにより、このようにパラメータを呼び出す手間が省けます。ただし、このパラメータを手動で変更することもできますが、構文は若干異なります。最後の行の「obj.func();」を次のように変更します:
// 出力 "Window の関連コンテンツ..."
// 出力 "Window の関連コンテンツ..."
上記の例からわかるように、call は実際には別の関数 (メソッド) です。 call は、obj.func のシステム組み込みメソッドに属します (JavaScript の特性によれば、関数はオブジェクトであることがわかっています)。
このように this が指すスコープを変更することで、innerFunc の this パラメータ、つまり「間違った」ポインティングを修正する例を引き続き使用できます:
toString:function(){ return 'obj のスコープ内 (範囲内)スコープ)';}, //console.log(this)
func: function(){
);
innerFunc.call(this); 🎜> }
};
obj.func();
// 出力 "obj のスコープ内 (スコープ内)"
// 出力 "obj のスコープ内 (スコープ内)スコープ)"
Ext
のスコープ設定 スコープを割り当てる関数はなく、その「this」がブラウザのウィンドウオブジェクト (イベントハンドラーなど) を指していることがわかります。 .) のポインタを変更しない限り、スコープはポインタのバインディングに使用できる設定項目です。関連する例については、Ext の createDelegate 関数
を参照してください。組み込みの call/apply メソッドと同様に、Ext はヘルパー メソッド createDelegate も提供します。この関数の基本的な機能は、このポインターをバインドすることですが、関数を createDelegate メソッドにパラメーターを渡すことですぐに実行しないようにすることです。例:
コードをコピーします。
コードは次のとおりです。
var obj = {
toString:function(){ return 'within the scope of obj (within the scope)';}, //Rewrite the toString function to facilitate the execution of console.log(this) Output
func: function(){
// The function here is directly subordinate to the object "object"
console.log(this);
var innerFunc = function(){
// nThe function here is not a direct member of a specific object, it is just a variable of another function
console.log(this);
};
innerFunc = innerFunc.createDelegate(this); // Here we use The delegated function overrides the original function.
. 🎜>// Output "within the scope of obj"
This is a small example, the principle is very basic, I hope you can digest it well. Despite this, in real work, we are still easily confused, but basically, if we can analyze the ins and outs according to the above theoretical knowledge, everything will change.
There is another thing, take a look at the following example:
Copy the code
for (var x = 0; x < col_length; x )
{
colarray[x] = varsDs.getAt(x).get('hex');
}
}}); However, it can be written more clearly:
var obj = {
callback: function(records){
col_length = varsDs.getCount();//here varDs went out of scope?
//col_length = this.getCount();//Is this equal to store?
// ...
}
};
varsDs.load(obj); Now the function callback is directly hung on obj, so the this pointer is equal to obj.
But note: This is useless. Why? Because you don't know what will happen when obj.callback is finally executed. Just imagine the load method of Ext.data.Store (imitation implementation):
Copy the code
The code is as follows: ... load : function(config) { var o = {};
o.callback = config.callback;
//Load
o.callback ();
}
...
In this fake implementation, the scope of the callback function is the private variable "o". Because you usually don't know how the function is called, if you don't declare the scope, you probably won't be able to use this parameter in the callback function.