넥스트 트리 사이트와 구글링을 통해 정리 해 보았습니다.
자세한 내용은
http://www.nextree.co.kr/p7650/
http://stackoverflow.com/questions/11187582/javascript-sandbox-pattern-example-implementation
/*First define the modules of the sandbox. These will be defined as properties on the constructor function because this is a convenient place to keep them.*/ Sandbox.modules = {}; Sandbox.modules.returnNumbers = function(MYAPP) { //MYAPP.return100 = function() {return 100;}; //return100처럼 익명 함수를 계속 구현하던지 그냥 이 공간에 바로 구현하던지.. 여기다가 바로 구현하고 // 아래 함수 호출시 // Sandbox('returnNumbers', 'returnLetters', function (MYAPP) { // }); // 이렇게 호출할 경우 모듈이 로드 되면서 바로 구현된 것들이 실행~ }; Sandbox.modules.returnLetters = function(MYAPP) { //MYAPP.returnABC = function() {return "ABC";}; }; function Sandbox() { /* Because Sandbox is a constructor, an new object is automatically created. Because we're in the constructor, we refer to this new object as 'this'. A constructor would typically be used as part of an assignment, e.g. myObject = new Sandbox(). However, it's also legitimate javascript to use a constructor without the assignment by just writing new Sandbox() with no assignment. The constructor does return an object, it's just that it doesn't get assigned to anything so is discarded. We're going to add functionality (methods) to the 'this' object, but rather than returning it, we will pass it to the callback function, so the methods can be used immediately. */ var args = Array.prototype.slice.call(arguments); //Put the arguments //of the call to the Sandbox constructor in an array called args. var callback = args.pop(); //The last argument is the callback var requiredmodules = args; //The remaining arguments are the require // modules //For each of the modules in 'requiredmodules', add the module's //methods to 'this' for (i=0; i< requiredmodules.length; i++) { Sandbox.modules[requiredmodules[i]](this); } //'this' now has methods returnNumbers and returnLetters //Call the callback. In the example below, 'this' will be called //MYAPP, which within the callback will have all the methods from //the required modules. callback(this); } //Finally here is an example of usage new Sandbox('returnNumbers', 'returnLetters', function (MYAPP) { console.log(MYAPP.return100()); console.log(MYAPP.returnABC()); });
'JS > JavaScript' 카테고리의 다른 글
Editor 개발 관련하여 괜찮은 스크립트 (0) | 2015.08.04 |
---|---|
[펌] 파이어폭스에서 마우스 드래그 막기 (0) | 2015.02.06 |
[링크]프로토타입의 정의를 아주 잘 설명해 놓은 글 입니다. (0) | 2014.12.24 |
XMLHttpRequest 를 이용한 파일 업로드 진행상태 보여주기 (0) | 2014.08.20 |
정규식 잘 정리된 사이트 (0) | 2013.12.27 |