|
1 | 1 | # JavaScript Class Implementation |
2 | 2 |
|
3 | | -This no longer works in IE 11. |
4 | | - |
5 | 3 | This implementation allows for classes to be given protected access to items in a super-class. |
6 | 4 |
|
| 5 | +This is a JavaScript module. |
| 6 | + |
7 | 7 | --- |
8 | 8 |
|
9 | 9 | ## Creating subclasses |
10 | 10 |
|
11 | | -**<samp style="background-color:transparent">*Class*.extend([*options*])</samp>** |
| 11 | +**<samp>*Class*.extend([*options*])</samp>** |
12 | 12 |
|
13 | 13 | Creates a new class that inherits from the parent class. |
14 | 14 |
|
15 | 15 | Parameters: |
16 | | -- *options* {object} |
| 16 | +- **<code>*options*</code>** {object} |
17 | 17 | This can include any of the following: |
18 | 18 |
|
19 | | - - className {string} |
20 | | - Used as `.name` for the class constructor and in `.toString()` for instances of the class. If not specified, it will be the same as the parent class. |
| 19 | + - **<code>className</code>** {string} |
| 20 | + Used for the `name` property of the class constructor, and in the `toString` method for instances of the class. If not specified, it will be the same as the parent class. |
21 | 21 |
|
22 | | - - constructorFn {function} |
23 | | - Initializes new instances of the class. A 'Super' function is passed as the first argument; <a href="#user-content-super">see below.</a> |
| 22 | + - **<code>constructorFn</code>** {function} |
| 23 | + Initializes new instances of the class.<br><br> |
| 24 | + **<samp>*options*.constructorFn(*Super*[, ...])</samp>**<br><br> |
| 25 | + **<code>*Super*</code>** {function} is to be called from inside `constructorFn` to initialize the class, using the class's parent's constructor. It should be called as soon as possible, before using the `this` keyword, to ensure that the instance is properly initialized.<br><br> |
| 26 | + Additionally, <code>*Super*</code> provides access to protected members (<a href="#protected-members">see below</a>). |
24 | 27 |
|
25 | | - - returnFn {function} |
26 | | - Returns a value when the constructor is called without using the 'new' keyword. |
27 | | - |
28 | | -### <span id="super">The 'Super' function</span> |
29 | | - |
30 | | -The first argument of the 'constructorFn' option is a function required to instantiate the class using the parent class' constructor. Basically, it acts like the 'super' keyword in ES6. It should be called as soon as possible inside the constructor, before using the 'this' keyword, to ensure that the instance is properly initialized. |
31 | | - |
32 | | -**<samp id="super">*options*.constructorFn(*Super*[, *arg1*[, ...]])</samp>** |
| 28 | + - **<code>returnFn</code>** {function} |
| 29 | + Returns a value when the constructor is called without using the `new` keyword. |
33 | 30 |
|
34 | 31 | #### Example |
35 | 32 |
|
@@ -73,7 +70,7 @@ s.whatAmI(); //I am a rectangle. I am a square. |
73 | 70 |
|
74 | 71 | ### Protected members |
75 | 72 |
|
76 | | -Additionally, a class can give its descendants protected access to its private variables. Once <code>*Super*()</code> is called within the constructor, the protected properties of its parent class are made available via <code>*Super*.protected</code>. This object will be available to child classes as well; any additions/deletions/overloads of its members that are made here in the constructor will be reflected in the class' descendants. |
| 73 | +A class can give its descendants protected access to its private variables. Once <code>*Super*</code> is called within the constructor, the protected properties of its parent class are made available via **<code>*Super*.protected</code>**. This object will be available to child classes as well; any additions/deletions/overloads of its members that are made here in the constructor will be reflected in the class' descendants. |
77 | 74 |
|
78 | 75 | #### Example |
79 | 76 |
|
@@ -131,12 +128,3 @@ let c = new Cuber(5); |
131 | 128 |
|
132 | 129 | c.cube(); //125 |
133 | 130 | ``` |
134 | | - |
135 | | - |
136 | | ---- |
137 | | - |
138 | | -## Avoid conflicts between scripts |
139 | | - |
140 | | -**<samp>Class.noConflict()</samp>** |
141 | | - |
142 | | -Restores `Class` to what it was before this script replaced it. The return value is this implementation of Class, so it can be assigned to another variable. |
0 commit comments