1, parameter default value”
function Point(x = 0, y = 0) {
this.x = x;
this.y = y;
}
const p = new Point();
2 and rest parameters
function add(...values) {
let sum = 0;
for (var val of values) {
sum += val;
}
return sum;
}
add(2, 5, 3);//Output 10
3, strict mode”
4, arrow function”
// Normal function writing
[1,2,3].map(function (x) {
return x * x;
});
// Arrow function writing
[1,2,3].map(x => x * x);
'use strict';
console.log(sum(1,2))
let sum = (a,b)=>{return a+b};
Attention should be paid to arrow functions when used.
Function bodythisObject is the object at which the definition is located, rather than the object when it is used.。
function foo() {
setTimeout(() => {
console.log('id:', this.id);
}, 100);
}
var id = 21;
foo.call({ id: 42 });
// id: 42
The ordinary function is:
function foo() {
setTimeout(function() {
console.log('id:', this.id);
}, 100);
}
var id = 21;
foo.call({
id: 42
});
//Output id:21
The arrow function does not have its own.this,Causing internalthisIt’s the outer block of code.this。
If the arrow function does not require parameters or requires more than one parameter, a parenthesis is used to represent the parameter part.
var f = () => 5;
// Equate to
var f = function () { return 5 };
var sum = (num1, num2) => num1 + num2;
// Equate to
var sum = function(num1, num2) {
return num1 + num2;
};