-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
50 lines (43 loc) · 1.37 KB
/
test.js
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
if (typeof global !== "undefined") // Are we in server mode?
require("./namespace.js"); // .. then include the namespace.js module
/**
* Example: Simple namespace declaration
*/
namespace.js("com.example.portal");
console.log(com.example.portal);
/**
* Examples: Simple usage
*/
namespace.js("com.example.portal.constants").CONSTANT_VALUE = 123;
console.log(com.example.portal.constants.CONSTANT_VALUE);
namespace.js("com.example.portal.dto").Animal = function (name) {
this.name = name;
this.getName = function () {
return this.name;
}
};
var animal = new com.example.portal.dto.Animal("Diesel");
console.log(animal.getName());
/**
* Example: Another possible style
*/
var i18n = namespace.js("com.example.portal.i18n");
i18n.en = {
hello: "Hello"
};
console.log(com.example.portal.i18n.en.hello);
/**
* Example: Check if namespace/object is defined
*/
var someDefinedObject = namespace.js("com.example.portal.dto.Animal", true);
var someUndefinedObject = namespace.js("com.example.portal.dto.SomeObject", true);
console.log(someDefinedObject);
console.log(someUndefinedObject);
/**
* Example: Define a namespace within another
*/
var someNamespaceRoot = namespace.js("com.example.portal");
namespace.js("math", false, someNamespaceRoot).randomNumber = function (min, max) {
return Math.random() * (max - min) + min;
};
console.log(com.example.portal.math.randomNumber(0, 100));