拼多多(8.11)前端笔试,JS代码 写出结果
有三道题 写输出结果的 已经跑过代码知道结果了,还是不明白为什么,求大佬解答
1、写输出
const obj={ a:1, fn:()=>{ console.log(this.a) } }; obj.fn(); //undefined这个题的结果是undefined 没明白=-=
2、写输出结果
Array.prototype.method = function() { console.log(this.length); }; var myArray= [1,2,3]; myArray.name = "array"; for (var value of myArray) { console.log(value); } for (var index in myArray){ console.log(myArray[index]); }这个题跑出来的结果如下
1
2
3
1
2
3
array
function(){
//...
}
function(){
console.log(this.length);
}
2
3
1
2
3
array
function(){
//...
}
function(){
console.log(this.length);
}
第一个console.log (var value of array) 输出的元素 1 2 3 理解没有问题
第二个console.log (var index in array) 输出结果为什么会有 array,以及后面的两个函数?
3、还是写输出结果
var num1= 1, num2=2; function cal() { var num1 = 10, num2= 20; console.log(this.num1 + this.num2); } var calBind = cal.bind({ num1: 100, num2: 200 }); new cal(); cal(); new calBind(); calBind();这个题的结果是
NaN
3
NaN
300
3
NaN
300
除了第二个输出3 其他的都不明白
(╯﹏╰) 求大佬指点迷津