1 | /* Javascript Object Inheritance Implementation ______ ________ |
||
2 | * (c) 2016 <[email protected]> __ / / __ \/ _/ _/ |
||
3 | * Licensed under MIT. / // / /_/ // /_/ / |
||
4 | * ------------------------------------------------------ \___/\____/___/__*/ |
||
5 | var JOII = require('../../dist/joii').JOII; |
||
6 | |||
7 | /** |
||
8 | * Tests instantiation of classes and the final result. Also tests integrity |
||
9 | * of getter/setter method-generation for public properties. |
||
10 | */ |
||
11 | test('ClassBuilder:InstantiationTest', function(assert) { |
||
12 | |||
13 | // Create a base class with 3 public properties. Note: If no visibility |
||
14 | // is specified, JOII assumes the property is public. Scalar properties are |
||
15 | // not referencable in JavaScript, therefore we're generating getters and |
||
16 | // setters for them instead. |
||
17 | var BaseClass = JOII.ClassBuilder({}, { |
||
0 ignored issues
–
show
|
|||
18 | a: 1, |
||
19 | b: "Foobar", |
||
20 | c: { 'hello' : 'world' } |
||
21 | }); |
||
22 | |||
23 | var a = new BaseClass(); |
||
24 | |||
25 | // Verify that all getters and setters are created successfully. |
||
26 | assert.equal(typeof(a.getA), 'function', 'Getter for property "a" exists.'); |
||
27 | assert.equal(typeof(a.getB), 'function', 'Getter for property "b" exists.'); |
||
28 | assert.equal(typeof(a.getC), 'function', 'Getter for property "c" exists.'); |
||
29 | assert.equal(typeof(a.setA), 'function', 'Setter for property "a" exists.'); |
||
30 | assert.equal(typeof(a.setB), 'function', 'Setter for property "b" exists.'); |
||
31 | assert.equal(typeof(a.setC), 'function', 'Setter for property "c" exists.'); |
||
32 | |||
33 | // The plain properties must not be defined in the instantiated class. |
||
34 | assert.equal(typeof(a.a), 'undefined', 'Actualy property "a" is undefined.'); |
||
35 | assert.equal(typeof(a.b), 'undefined', 'Actualy property "b" is undefined.'); |
||
36 | assert.equal(typeof(a.c), 'undefined', 'Actualy property "c" is undefined.'); |
||
37 | |||
38 | // Test integrity of value retrieval using getters. |
||
39 | assert.strictEqual(a.getA(), 1, 'Value of property "a" using getter is OK.'); |
||
40 | assert.strictEqual(a.getB(), "Foobar", 'Value of property "b" using getter is OK.'); |
||
41 | assert.deepEqual(a.getC(), { 'hello' : 'world' }, 'Value of property "c" using getter is OK.'); |
||
42 | |||
43 | // Test updating a property using the setter and retrieving it again using |
||
44 | // the getter for the associated property. |
||
45 | a.setA(2); |
||
46 | a.setB('foo'); |
||
47 | a.setC({'foo' : 'bar'}); |
||
48 | |||
49 | // Verify that the updated values are set correctly. |
||
50 | assert.strictEqual(a.getA(), 2, 'Value of property "a" is updated correctly.'); |
||
51 | assert.strictEqual(a.getB(), 'foo', 'Value of property "b" is updated correctly.'); |
||
52 | assert.deepEqual(a.getC(), {'foo':'bar'}, 'Value of property "c" is updated correctly.'); |
||
53 | |||
54 | var fluid = JOII.ClassBuilder({ |
||
55 | |||
56 | a: function() { |
||
57 | return this.__api__; |
||
58 | }, |
||
59 | |||
60 | b: function() { |
||
61 | return this.__api__; |
||
62 | } |
||
63 | |||
64 | }); |
||
65 | |||
66 | var f = new fluid(); |
||
67 | |||
68 | assert.strictEqual(typeof(f.__api__), 'undefined', '__api__ is undefined in public scope'); |
||
69 | assert.strictEqual(typeof(f.a()), 'object', 'f.a returns an object.'); |
||
70 | assert.strictEqual(typeof(f.a().b), 'function', 'f.a.b is a function.'); |
||
71 | assert.strictEqual(typeof(f.a().b().a), 'function', 'f.a.b.a returns a function.'); |
||
72 | |||
73 | // 3.1.0: Custom constructors |
||
74 | JOII.Config.addConstructor('main'); |
||
75 | var xx,xxx; |
||
76 | |||
77 | xx = JOII.ClassBuilder({ a: 0, main: function () { this.a = 1; }}); xxx = new xx(); |
||
78 | assert.strictEqual(xxx.getA(), 1, 'Custom constructor "main" called.'); |
||
79 | xx = JOII.ClassBuilder({ a: 0, construct: function () { this.a = 1; }}); xxx = new xx(); |
||
80 | assert.strictEqual(xxx.getA(), 1, 'New default constructor "construct" called.'); |
||
81 | xx = JOII.ClassBuilder({ a: 0, '->': function () { this.a = 1; }}); xxx = new xx(); |
||
82 | assert.strictEqual(xxx.getA(), 1, 'New default constructor "->" called.'); |
||
83 | xx = JOII.ClassBuilder({ a: 0, '=>': function () { this.a = 1; }}); xxx = new xx(); |
||
84 | assert.strictEqual(xxx.getA(), 1, 'New default constructor "=>" called.'); |
||
85 | xx = JOII.ClassBuilder({ a: 0, __construct: function () { this.a = 1; }}); xxx = new xx(); |
||
86 | assert.strictEqual(xxx.getA(), 1, 'Original constructor "__construct" called.'); |
||
87 | }); |
||
88 |
Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.
Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.
We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.