2012-02-08 4 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

고마워요. 이것은 정말로 도움이된다. –

관련 문제