@@ -113,3 +113,53 @@ var inherit = (function(){
113
113
}
114
114
})();
115
115
```
116
+
117
+ ## klass
118
+
119
+ * some legacy JS libraries/frameworks emulate classes
120
+ * usually there's a convention on how to name constructor functions (e.g. init)
121
+ * they tend to support classical inheritance
122
+
123
+ ## prototypal inheritance
124
+
125
+ * modern classless pattern
126
+ * no classes, objects inherit from objects
127
+
128
+ ``` js
129
+ function object (parent ) {
130
+ function F () {}
131
+ F .prototype = parent;
132
+ return new F ();
133
+ }
134
+
135
+ var parent = { ... }
136
+ var child = object (parent);
137
+ ```
138
+
139
+ * children get parent methods and properties via ``` __proto__ ``` link
140
+ * parent can be created via constructor as well (not just literal)
141
+
142
+ ## Object.create
143
+
144
+ * prototypal inheritance is built-in since ES5
145
+ * ``` Object.create(parentObject, ownPropertiesObject) ```
146
+
147
+ ## inheritance by copying properties
148
+
149
+ ``` js
150
+ // shallow copy
151
+ function extend (parent , child ) {
152
+ var key;
153
+ child = child || {};
154
+ for (key in parent){
155
+ if (parent .hasOwnProperty (key)){
156
+ child[key] = parent[key];
157
+ }
158
+ }
159
+ return child;
160
+ }
161
+ ```
162
+
163
+ * shallow copy just copies references of arrays and object
164
+ * children can modify parent properties :(
165
+ * deep copy is when array elements and object properties are copied as well
0 commit comments