答案再下面不做解析
console.log(1 || 2) // 输出?
console.log(1 && 2) // ?
function test() {
console.log("打印");
}
console.log(test && test());
function test() {
console.log("打印");
return 1;
}
console.log(test && test());
let myObj = {
name:"我是一级name",
listObj:{
name:"我是二级name",
test:function(){
console.log(this.name);
}
}
}
console.log(myObj && myObj.listObj.test());
let myObj = {
name: "我是一级name",
sanObj: {
name: "我是二级name",
test: () => {
let test1 = () => {
console.log(this.name);
};
test1();
},
},
};
console.log(myObj && myObj.sanObj.test());
1、这里为1和2,为啥第二个是2呢?,&& 咱们常写判断中 谁&&谁为true才走下面逻辑,这里则是从第一个判断1为true则输出第二个值也就是2,如果第一个为false 则直接输出false,有疑问的已经可以重新学习一下js基础了(开玩笑稍微去了解了解^_^)
2、根据上面,这个应该打印出的是什么?
打印 undefined test函数没有返回值,所以返回undefined
3、这个不需解释打印出1
4、这个呢?考察各位this指向问题,这个打印则是我是二级name
5、 undefined 箭头函数没有自己的this,this指向上下文这里this指向的是window window.name为undefined(node环境下,如果浏览器环境下的window.name = "")