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 inheritance of instantiated class defintions. |
||
9 | */ |
||
10 | test('ClassBuilder:AbstractImplementationTest', function(assert) { |
||
11 | |||
12 | // An abstract property must also have a functional one in the same class. |
||
13 | assert.throws(function() { |
||
0 ignored issues
–
show
|
|||
14 | var a = JOII.ClassBuilder({}, { 'abstract public function test' : function() {} }); new a(); |
||
15 | }, function(err) { return err === 'Missing abstract member implementation of test()'; }, 'Validate: Missing implementation of abstract properties.'); |
||
16 | |||
17 | // An abstract property must be implemented by a child class. |
||
18 | assert.throws(function() { |
||
19 | var a = JOII.ClassBuilder({}, { 'abstract public function test' : function() {} }); |
||
20 | var b = JOII.ClassBuilder({ 'extends': a }, {}); new b(); |
||
21 | }, function(err) { return err === 'Missing abstract member implementation of test()'; }, 'Validate: Missing implementation of abstract properties.'); |
||
22 | |||
23 | // Visibility of an abstract property may not change. |
||
24 | assert.throws(function() { |
||
25 | var a = JOII.ClassBuilder({}, { 'abstract public function test' : function() {} }); |
||
26 | var b = JOII.ClassBuilder({ 'extends': a }, { 'protected function test' : function() {} }); new b(); |
||
27 | }, function(err) { return err === 'Member "test" must be public as defined in the parent class.'; }, 'Validate: Visibility change of abstract implementation.'); |
||
28 | |||
29 | // This should _not_ throw an exception. |
||
30 | var a = JOII.ClassBuilder({}, { 'abstract public function test' : function() {} }); |
||
31 | var b = JOII.ClassBuilder({ 'extends': a }, { 'public function test' : function() {} }); new b(); |
||
32 | |||
33 | // Defining an abstract and functional property in the same class is valid. (beware of declaration order) |
||
34 | var c = JOII.ClassBuilder({}, { |
||
35 | 'abstract public function test' : function() {}, |
||
36 | 'public function test' : function() {} |
||
37 | }); |
||
38 | new c(); |
||
39 | |||
40 | // Implement abstract property in the middle of the inheritance chain. |
||
41 | var c1 = JOII.ClassBuilder({}, { 'abstract public function test' : function() {} }); |
||
42 | var c2 = JOII.ClassBuilder({ 'extends' : c1 }, {}); |
||
43 | var c3 = JOII.ClassBuilder({ 'extends' : c2 }, { 'public function test' : function() {} }); |
||
44 | var c4 = JOII.ClassBuilder({ 'extends' : c3 }, {}); |
||
45 | |||
46 | // Should not throw exception for missing an abstract implementation. |
||
47 | new c4(); |
||
48 | }); |
||
49 |
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.