Java Script의 모듈 패턴에 대한 코드 프로젝트의 작은 기사를 읽었습니다. 자바 스크립트 코드를 읽은 후 코드가 작성된 방식이 명확하지 않습니다. 나는 자바 스크립트를 썼지 만, 나는 사전에 자바 스크립트를 작성하는 것에 익숙하지 않다. 여기에 내가 CodeProject의 http://www.codeproject.com/Articles/619747/Module-Pattern-in-Java-Script-in-DepthJavaScript의 모듈 패턴에 관한 정보
1)에서 읽은 URL을 추가 할 수
을위한 새로운 기능을 생성하는 기능은 //입니다function addGenerator(num) {
// Return a simple function for adding two numbers
// with the first number borrowed from the generator
return function (toAdd) {
return num + toAdd
};
}
// addFive now contains a function that takes one argument,
// adds five to it, and returns the resulting number
var addFive = addGenerator(5);
// We can see here that the result of the addFive function is 9,
// when passed an argument of 4
alert(addFive(4) == 9); // Which return true
addGenerator 후 5 인수로 전달되었지만 난 그냥 이렇게 호출 어떻게 작동하는지이 줄을 이해하지 못합니다.
return function (toAdd) {
return num + toAdd
};
addGenerator (5)는 무엇을 반환할까요?
이것이 true를 반환하는 방법 -> alert (addFive (4) == 9); // true를 돌려
2)
위의 코드는 &가 호출됩니다 작동 방법var EmployeeModule = (function (my) {
my.anotherFunction = function() {
return alert('this is another function.');
};
} (EmployeeModule));
? 그들이하려는 일에 대해 상세하게 expalin 해주시겠습니까? 난 그냥이 라인 (EmployeeModule || {}))
을 이해하지 못하는
var EmployeeModule = (function (my) {
// add functionality...
return my;
}(EmployeeModule || {}));
3)이 라인의 의미를 설명해주십시오.
4) 모듈 패턴
글로벌 가져 오기 우리는 또한 우리가 하위 모듈을 만들 수 많은 경우가 있습니다
(function ($, Y) {
// now have access to globals jQuery (as $) and YAHOO (as Y) in this code
}(jQuery, YAHOO));
Sub-modules in Module Pattern
우리의 모듈에서 다른 자바 스크립트 라이브러리를 가져올 수 있습니다. 일반 모듈을 만드는 것과 같습니다. 접기 | 코드 복사가
EmployeeModule.subModule = (function() {
var my = {};
// ...
return my;
}());
나은 설명에 더 예를 현명 위의 코드 포인트에 대한 좋은 설명을 찾고. 감사합니다
타입 주조, 단락 연산자, 즉시 호출 된 함수 식 (IIFE), 클로저, 고차 함수 및 부분적 응용 프로그램과 같은 모든 코드를 이해하는 데 필요한 개념이 있다고 생각합니다. – elclanrs