1.阮一峰老师的es6中说对象解构赋值可以取到继承的属性。同样,也可以取到继承的方法
const obj1 = {};
const obj2 = { fooo: 'bar', hello: function(){ console.log("hello")} };
Object.setPrototypeOf(obj1, obj2);
const { fooo, hello: helloo } = obj1;
console.log(fooo); // "bar"
helloo(); //hello
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayName = function () {
console.log("i am " + this.name);
}
function Student(name, age, grade) {
Person.call(this, name, age);
this.grade = grade;
}
Student.prototype = new Person();
const s1 = new Student("s1", 1, 1);
s1.sayName();
const { sayName: sayname } = s1;
console.log(sayname, s1);
sayname.call(s1);
2.函数参数也可以使用默认值,解构失败时,x和y等于默认值。指定参数有两种不同的情况。
私认为,第一种情况时,x、y为两个分开的变量,指定的默认值互相区分。第二种情况时,x、y两个参数集合成一个整体,为move的参数对象,所以不给其中一个变量赋值时为undefined。当参数整体不存在时,才使用整体参数的默认值。
// 1- 为变量x&y指定默认值
function move({ x = 0, y = 0 } = {}) {
return [x, y];
}
console.log((move({ x: 3, y: 8 })) instanceof Array); // [3, 8]
console.log(move({ x: 3 })); // [3, 0]
console.log(move({})); // [0, 0]
console.log(move()); // [0, 0]
console.log("-------------------");
// 2 - 为move的参数指定默认值
function move({ x, y } = { x: 0, y: 0 }) {
return [x, y];
}
console.log(move({ x: 3, y: 8 })); // [3, 8]
console.log(move({ x: 3 }));; // [3, undefined]
console.log(move({}));; // [undefined, undefined]
console.log(move());; // [0, 0]
773

被折叠的 条评论
为什么被折叠?



