defineModule.js
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @fileoverview Define module
* @author NHN.
* FE Development Lab <dl_javscript@nhn.com>
* @dependency type.js, defineNamespace.js
*/
'use strict';
var defineNamespace = require('./defineNamespace');
var type = require('./type');
var INITIALIZATION_METHOD_NAME = 'initialize';
/**
* Define module
* @param {string} namespace - Namespace of module
* @param {Object} moduleDefinition - Object literal for module
* @returns {Object} Defined module
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var myModule = util.defineModule('modules.myModule', {
* name: 'john',
* message: '',
* initialize: function() {
* this.message = 'hello world';
* },
* getMessage: function() {
* return this.name + ': ' + this.message
* }
* });
*
* console.log(myModule.getMessage()); // 'john: hello world';
*/
function defineModule(namespace, moduleDefinition) {
var base = moduleDefinition || {};
if (type.isFunction(base[INITIALIZATION_METHOD_NAME])) {
base[INITIALIZATION_METHOD_NAME]();
}
return defineNamespace(namespace, base);
}
module.exports = defineModule;