看 《 深入理解es6》 一书,有些知识点没有记录下来。 但大部分的知识点记录下来了。
// 函数的一个特性
function Person(name) {
this.name = name;
}
var person = new Person("jack");
var notAPerson = Person("jack");
console.log(person); // "[Object object]"
console.log(notAPerson); // "undefined"
//js函数中有两个不同的内部方法, [[Call]] 和 [[Construct]] 当通过new 关键字调用函数的时,执行的是[[Construct]]函数,它负责他负责建立一个通常被称作实例的新对象,然后在执行函数体,将this绑定到实例对象中。如果不通过则使用[[Call]]函数,从而直接执行代码中的函数体。 具有[[Construct]]方法的函数被统称为构造函数
// 注: 不是所有的函数都有[[Construct]],箭头函数就没有,故箭头函数不能被new 来调用
// 默认参数对arguments的影响
//在es5中 非严格模式下 参数的改变会直接改变arguments的值
// 严格模式下 参数的改变不会影响arguments的值
function mixArgs(first, second) {
console.log(first === arguments[0]); // true
console.log(second === arguments[1]); // true
first = 'c';
second = 'd';
console.log(first === arguments[0]); // true 严格模式 false es6中 false
console.log(second === arguments[1]); // true 严格模式 false es6中 false
}
mixArgs('a', 'b');
// 这个是判断 是否为new运算符出来的实例 对象
// 在函数外使用new.target 是一个语法错误
function Person(name) {
// new.target
if(typeof new.target === Person){
this.name = name;
}
}
// 箭头函数
// 1. 没有this、super、arguments和new.target绑定 (这些值由外围的非箭头函数所决定)
// 2. 不能通过new关键字调用
// 3. 没有原型 (因为不能通过new关键字来调用箭头函数)
// 4. 不可以改变this的绑定
// 5. 不支持 arguments 对象
// 6. 不支持重复的命名函数
// 注: 箭头函数的name属性和其他函数的规则相同
// 箭头函数没有arguments属性
// let person = () => arguments[0]
// console.log(person(5))
这章节有默认参数的临时死区、无名参数、元属性(new.target)等