2012-02-08 2 views
0

完全なコードはここで見つけることができますどのように説明することができます:http://www.webdeveloper.com/forum/showthread.php?t=224180#6これは機能のようにjqueryのをエミュレートします。次の部分は方法を定義しますは、誰かがこの部分

//define some methods for the utility: 
var meths={ 
    hide:function(a){if(a)a.style.display="none";}, 
    show:function(a){if(a)a.style.display="";}, 
    remove:function(a){if(a)a.parentNode.removeChild(a);}, 
    color:function(a){a.style.color=this||a.style.color;}, 
    size:function(a){if(a)a.style.fontSize=this||a.style.fontSize;} 

};//end meths 

私は上記の部分を取得します。これは、もちろん、閉鎖の一部です。私は、次の部分を理解しているようだとX.hide()を呼び出す方法を誰かがこの

//bind the methods to the collection array: 
for(var meth in meths) 
    (function(n,m){ 
    output[n]=function(x){output.map(m,x); return output;} 
    }(meth, meths[meth]));//next method 

return output; 
+0

説明するために、時間がかかる場合はXに対応するメソッドを呼び出すことはできませんどのようにこの部分何? –

答えて

2
// Earlier in the code, 'output' was defined as an array... 
var output = []; 

// For each key in the object 'meths'... 
// (which was defined as an object of methods) 
for (var meth in meths) 
    // Execute the closure... (using the key as 'n' and the method as 'm') 
    (function(n,m){ 
     // Using the key, assign to the 'output' array a function that 
     // maps the current method that we're on, for each element in the array. 
     // (Array.map() runs a function, given as the first argument, for each 
     // element in an array) 
     // 
     // In this case, the 'x' in the function is the placeholder for a 
     // parameter. Like jQuery, this returns the modified output for chaining. 
     output[n] = function(x){ output.map(m,x); return output; }; 
    })(meth, meths[meth]); // Using the key and method 

return output; 
+1

+1。 OTではなく、ループ外でその関数を定義する方が良いでしょう。 'meths.hasOwnProperty(meth)' – Phil

+0

もありがとうございます。これは本当に役立ちます –

関連する問題