拼多多(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);
}

第一个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  其他的都不明白

(╯﹏╰)  求大佬指点迷津







#拼多多##笔试题目#
全部评论
var num1 = 1, num2 = 2; function cal() {     var num1 = 10, num2 = 20;     console.log('this', this);     console.log('----------');     console.log('this.num1', this.num1);     console.log('----------');     console.log(this.num1 + this.num2); } var calBind = cal.bind({ num1: 100, num2: 200 });   new cal(); cal(); new calBind(); calBind(); // NaN      // new 创建对象,此时的 this 指向 cal , 但 var num1 并不是定义 this.num1 。所以最后就是 undefined + undefined = NaN // 3        // 此时的 this 指向外部 window ,window 中定义了 var num1 = 1, var num2 = 2;        // node 跑出来的结果是 NaN;浏览器跑出来的结果是 3 // NaN      // new 创建对象,此时的 this 还是指向 cal ,undefined + undefined = NaN // 300      // bind 绑定函数 this ,此时 100 + 200 = 300
点赞 回复
分享
发布于 2019-08-12 11:51
第一个是this指向全局作用域
点赞 回复
分享
发布于 2019-08-11 19:32
联易融
校招火热招聘中
官网直投
这是内推吗
点赞 回复
分享
发布于 2019-08-11 19:35
箭头函数的this指向定义该函数时所在的作用域,所以指向window
点赞 回复
分享
发布于 2019-08-11 19:37
1.考察了箭头函数的this,是箭头函数外面的第一个非箭头函数的this,在这是全局 2.for...in语句以任意顺序遍历一个对象自有的、继承的、可枚举的、非Symbol的属性。(来自mdn),这里原型上继承的method也遍历出来了 数组是对象 本质是 [0:0,1:1,2:2,....] 3不确定等一个大佬发言
点赞 回复
分享
发布于 2019-08-11 19:53
3. new cal()应该是考查new的实现,构造函数绑定一个空对象,结果是undefined+undefined;第二个是this指向全局,第三个我估摸着也是因为new的时候内部给它重新绑定this了
点赞 回复
分享
发布于 2019-08-11 20:25
对象不构成作用域所以箭头函数的this是全局this,for...of遍历数组元素内容,for...in会遍历出所有可枚举元素,包括定义的name和函数。第三题不太明白😅
点赞 回复
分享
发布于 2019-08-11 20:39
拼多多这次笔试大概什么时候有结果呢
点赞 回复
分享
发布于 2019-08-12 13:05

相关推荐

1 21 评论
分享
牛客网
牛客企业服务