✍️ Tangxt | ⏳ 2020-06-01 | 🏷️ 课后作业 |
add(1); //1
add(1)(2); //3
add(1)(2)(3); //6
add(1)(2,3); //6
add(1,2)(3); //6
add(1,2,3); //6
不会……
let obj = {
2: 3,
3: 4,
length: 2,
push: Array.prototype.push
}
obj.push(1);
obj.push(2);
console.log(obj);
分析:
测试正确
➹:实现JS原生数组的shift、unshift、pop、push · Issue #8 · ziwei3749/blog
var a = ?;
if (a == 1 && a == 2 && a == 3) {
console.log('OK');
}
不会……
function C1(name) {
if (name) {
this.name = name;
}
}
function C2(name) {
this.name = name;
}
function C3(name) {
this.name = name || 'join';
}
C1.prototype.name = 'Tom';
C2.prototype.name = 'Tom';
C3.prototype.name = 'Tom';
alert((new C1().name) + (new C2().name) + (new C3().name));
解析:
测试正确
function Foo() {
getName = function () {
console.log(1);
};
return this;
}
Foo.getName = function () {
console.log(2);
};
Foo.prototype.getName = function () {
console.log(3);
};
var getName = function () {
console.log(4);
};
function getName() {
console.log(5);
}
Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();
分析:
测试,后边俩行代码计算错误 -> 我想这考察的是new
的优先级吧!
function fun(){
this.a=0;
this.b=function(){
alert(this.a);
}
}
fun.prototype={
b:function(){
this.a=20;
alert(this.a);
},
c:function(){
this.a=30;
alert(this.a)
}
}
var my_fun=new fun();
my_fun.b();
my_fun.c();
分析:
测试正确
画图姿势归纳:
prototype
:用橙色文字__proto__
:用红色文字0xff……
0x1……
let n = 10;
let m = n.plus(10).minus(5);
console.log(m);//=>15(10+10-5)
做法:
Number.prototype.plus = function plus(n) {
let num = this.valueOf() + n;
return new Number(num);
};
Number.prototype.minus = function minus(n) {
let num = this.valueOf() - n;
return num;
};