代码类面试题

1.this的指向


function Cat (name) {

    this.name = name;

}

Cat.prototype.sayCatName = () => {

    console.log(this === window);

    return this.name;

};


const cat = new Cat('Miao');


cat.sayCatName();

 
//结果为:true

2.let和var定义变量

for (var i = 0; i < 3; i++) {
 setTimeout(() => console.log(i), 1);
}
for (let i = 0; i < 3; i++) {
 setTimeout(() => console.log(i), 1);
}
//结果
3 3 3
0 1 2

3.对象的引用的是对象的地址

let c = { greeting: "Hey!" };
let d;
d = c;
c.greeting = "Hello";
console.log(d.greeting);
//结果:
hello

4.this在箭头函数中的指向

function Person(firstName, lastName) {
 this.firstName = firstName;
 this.lastName = lastName;
}
const member = new Person("Lydia", "Hallie");
Person.getFullName = () => this.firstName + this.lastName;

console.log(member.getFullName());
//结果
member.getFullName is not a function
    at <anonymous>:8:20
报错该对象没有这个方法

5.对象进行===时的情况

function checkAge(data) {
 if (data === { age: 18 }) {
 console.log("You are an adult!");
 } else if (data == { age: 18 }) {
 console.log("You are still an adult.");
 } else {
 console.log(`Hmm.. You don't have an age I guess`);
 }
}
checkAge({ age: 18 });
//结果:You don't have an age I guess
进入了else
勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!
原文地址:https://www.cnblogs.com/qiaozhiming123/p/14710884.html