Advanced JavaScript

This article records Javascript questions that I think is important but often ignored by people.
JavaScript


Array.prototype.map()

1
2
3
['1', '2', '3'].map(parseInt)

what & why ?

The map() method creates a new array with the results of calling a provided function on every element in the calling array. See more in Array.prototype.map() | MDN.

1
2
const arr = [1, 2, 3];
arr.map((num) => num + 1); // [2, 3, 4]

For each iteration map, parseInt() passes two parameters: string and index. So the code actually executed is:

1
2
3
['1', '2', '3'].map((item, index) => {
return parseInt(item, index)
})

This will return

1
2
3
parseInt('1', 0) // 1
parseInt('2', 1) // NaN
parseInt('3', 2) // NaN, 3 cannot be divided by 2

So:

1
2
['1', '2', '3'].map(parseInt)
// 1, NaN, NaN

In order to achieve the function we want, we could do this:

1
2
3
4
5
['1', '2', '3'].map(Number)

OR

['1','2','3'].map(n => parseInt(n,10))

extends

1
Difference of extends method between ES5 and ES6 

ES5

  • Prototype Inheritance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Parent
function Parent (name) {
this.name = name;
}

Parent.prototype.getName = function () {
return this.name;
}

// Children
function Children() {
this.age = 24;
}

// Prototype extension
Children.prototype = new Parent('James');

// Children.prototype.constructor === Parent.prototype.constructor = Parent
var test = new Children();

// test.constructor === Children.prototype.constructor === Parent

test.age // 24
test.getName(); // James

We can find that the whole inheritance process is delegated through the pointing between prototype chains until the final result of “constructed by constructors” is formed.

  • Constructor Inheritance
1
2
3
4
5
6
7
8
9
10
11
12
// Parent
function Parent (name) {
this.name = name;
}

function Children () {
Parent.apply(this, arguments);
}

var test = new Children('Kiki');

test.name // Kiki

The key of constructor inheritance is to get the members and methods of the parent class on the newly created objects in the future by calling the parent class inside the child class, that is, by using the apply() or call() method.

ES6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Father {
constructor(name, age) {
this.name = name;
this.age = age;
}

show() {
console.log(`Name:${this.name}, Age: ${this.age}`);
}
};

class Son extends Father {};
let son = new Son('James', 24);

son.show(); // Name: James, Age: 24
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2018-2024 James Wang
  • Visitors: | Views:

Buy me a coffee~

支付宝
微信