: 여러 구성 요소를 사용하여 응용 프로그램 코드를 작성하는 경우 마찬가지로
(function() {
var comp = {};
// Export our component
myProject.components.myComponent = comp;
// add things to `comp`; since `comp` and `myProject.components.myComponent`
// refer to the same object, adding to `comp` is adding to the one object
// they both refer to and so you can access those things via either reference
})();
:
예, 코드는 myComponent
구성 요소를 정의 물건
var myComp = myProject.components.myComponent;
myComp.func1();
이것은 단지 참조 용입니다. 당신은 다른 긴 이름이 작업을 수행하고 또한 당신이 당신의 코드를
// 파일 정리 RequireJS과 코드를 구성 할 수 있습니다 덜이
var getEl = document.getElementById,
myEl = getEl('divId');
처럼 쓸 수 있습니다 : MyProject를/구성 요소/myComponent.js을
define(function(){
var myComponent ={};
myComponent.func1 = function(){
...
}
return myComponent;
});
// 파일 : MyProject를/main.js
require(['myProject/components/myComponent', 'myProject/components/myComponent2'],
function(comp1, comp2){
var main = {};
main.init = function() {
...
comp1.func1();
}
});
// 파일 : myPro ject/index.html
이것은 아마도 가장 보편적 인 해결책 일 것입니다. (단지 "해야"하는 것이 의미가있는 이유에 대해). 범위 지정 함수는 네임 스페이스 충돌을 방지하여 창 개체를 다시 정의해야 나중에 코드에서 재난을 초래할 수 있습니다. – Shane