Skip to content

Commit 646a43e

Browse files
committed
Accept Merge Request InterviewMap#32: (lgc-inheritance -> master)
Merge Request: add inheritance Created By: @amandakelake-lgc Reviewed By: @13221097537 Approved By: @13221097537 Accepted By: @13221097537 URL: https://coding.net/u/mart_13221097537/p/Front-End-Interview-Map/git/merge/32
2 parents 2b19b43 + 25b5048 commit 646a43e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

JS/JS-en.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,69 @@ Each object has an internal property, denoted as `__proto__` , which is a refere
245245
Objects can use `__proto__` to find properties that do not belong to the object, and `__proto__` connects objects together to form a prototype chain.
246246

247247

248+
#### inheritance
249+
250+
In ES5, we can solve the problems of inheritance by using the following ways.
251+
252+
```js
253+
function Super() {}
254+
Super.prototype.getNumber = function() {
255+
return 1
256+
}
257+
258+
function Sub() {}
259+
let s = new Sub()
260+
Sub.prototype = Object.create(Super.prototype, {
261+
constructor: {
262+
value: Sub,
263+
enumerable: false,
264+
writable: true,
265+
configurable: true
266+
}
267+
})
268+
```
269+
270+
The above idea of inheritance implementation is to set the `prototype` of the child class as the `prototype` of the parent class.
271+
272+
In ES6, we can easily solve this problem with the `class` syntax.
273+
274+
```js
275+
class MyDate extends Date {
276+
test() {
277+
return this.getTime()
278+
}
279+
}
280+
let myDate = new MyDate()
281+
myDate.test()
282+
```
283+
284+
However, ES6 is not compatible with all browsers, so we need to use Babel to compile this code.
285+
286+
If call `myDate.test()` with compiled code, you’ll be surprised to see that there’s an error
287+
288+
![](https://user-gold-cdn.xitu.io/2018/3/28/1626b1ecb39ab20d?w=678&h=120&f=png&s=32812)
289+
290+
Because there are restrictions on the low-level of JS, if the instance isn’t constructed by `Date` , it can’t call the function in `Date`, which also explains on the side: `Class` inheritance in ES6 is different from the general inheritance in ES5 syntax.
291+
292+
Since the low-level of JS limits that the instance must be constructed by `Date` , we can try another way to implement inheritance.
293+
294+
```js
295+
function MyData() {
296+
297+
}
298+
MyData.prototype.test = function () {
299+
return this.getTime()
300+
}
301+
let d = new Date()
302+
Object.setPrototypeOf(d, MyData.prototype)
303+
Object.setPrototypeOf(MyData.prototype, Date.prototype)
304+
```
305+
306+
The Implement ideas of the above inheritance: firstly create the instance of parent class => change the original `__proto__` of the instance, connect it to the `prototype` of child class => change the `__proto__` of child class’s `prototype` to the `prototype` of parent class.
307+
308+
The inheritance implement with the above method can perfectly solve the restriction on low-level of JS.
309+
310+
248311
#### Promise implementation
249312
250313
`Promise` is a new syntax introduced by ES6, which resolves the problem of callback hell.

0 commit comments

Comments
 (0)