增强 Function 构造函数
ES6 中可以使用【默认参数】和【不定参数】
// ES6 增强 Function 构造函数
const add = new Function('first', 'second', 'return first + second')
console.log(add(1, 1))
// 可以使用【默认参数】和【不定参数】
var minue = new Function('first', 'second = first', "return first - second")
var pickFirst = new Function('...args', "return args[0]")
展开运算符
可以通过【...】将 数组或者对象 拆解为独立参数项
// ES5
let values = [12, 23, 45, 12, 13]
console.log(Math.max.apply(Math, values))
// ES6
console.log(Math.max(...values))
name属性
ES6 中所有的函数都具备 name 属性
- bind() 函数的 name 会添加 bound 前缀
- new Function() 函数的 name 会为 anonymous
注意:函数 name 属性不一定引用同名变量,所以不能使用其来获取函数的引用
function doSomething() {
// ...
}
var doAnoteher = function () {
// ...
}
console.log(doSomething.name) // doSomething
console.log(doAnoteher.name) // doAnoteher
// ------------------------------------
var doSomething = function () {
// ...
}
var person = {
get firstName() {
return "Nicholas"
},
sayName: function () {
console.log(this.firstName)
}
}
console.log(doSomething.name) // doSomething
console.log(person.sayName.name) // sayName
console.log(person.firstName.name) // underfined
// --------------------------------------------
var doSomething = function () {
// ...
}
console.log(doSomething.bind().name) // "bound doSomething"
console.log((new Function()).name) // "anonymous"