Tencent front-end interview questions (1)

1. There is a class as follows:

function Person(name) {
    this.name = name
}

let p = new Person('Tom');

1. What is p._proto_ equal to?

Answer: Person.prototype

2. What is Person._proto_ equal to?

Answer: Function.prototype

Analysis:

1, 2 are the same problem, both of which are related to the knowledge of the prototype chain. Just remember one sentence: the _proto_ property (prototype) of an instance is equal to the prototype property of its constructor . The constructor of the instance p is Person, and the constructor of Person is Function.

comprehend by analogy

var f = {}
    F = function (){};
Object.prototype.a = 'value a';
Function.prototype.b = 'value b';

console.log(f.a)
console.log(f.b)
console.log(F.a)
console.log(F.b)

result:

value a;

undefined;

value a;

value b;

Analysis:

f is an instance of Object, F is an instance of Function, the properties of the object will be looked up layer by layer along the prototype chain, if not found, it will be undegined

3. If you change the stem to

function Person(name) {
    this.name = name
    return name
}
let p = new Person('Tom')

During the instantiation of Person, what does Person return (or what is p equal to)?

answer:

 {name: "Tom"}

4. If you change the stem to

function Person(name) {
    this.name = name
    return {}
}
let p = new Person('Tom');

During the instantiation of Person, what does Person return (or what is p equal to)?

{}

Analysis:

The constructor does not require an explicit return value. When using new to create an object (calling the constructor), if the return is a non-object (number, string, boolean, etc.), the return value will be ignored; if the return is an object, the object will be returned (Note: if return null is also Ignore the return value).

5. The difference between typeof and instanceof

In JavaScript, the typeof operator is often used to determine the type of a variable. When using the typeof operator to store the value using a reference type, there will be a problem. No matter what type of object is referenced, it will always return'object'.

The instanceof operator is used to test whether an object has a prototype property of a constructor in its prototype chain. Syntax: object instanceof constructor Parameters: object (object to be detected) constructor (a constructor) Description: The instanceof operator is used to detect whether constructor.prototype exists on the prototype of the parameter object.

6. If Student inherit from Person (Student class inherits Person, it needs to be inheritance based on prototype), let s = new Student('Lily'), then what does s instanceof Person return?

function Person(name) {
    this.name = name
}
function Student() {

}

Student.prototype = Person.prototype;
Student.prototype.constructor = Student;

let s = new Student('Tom');
console.log(s instance Person);

Answer: true

7. The internal mechanism of new and instanceof

1. 创建一个新对象,同时继承对象类的原型,即Person.prototype;
2. 执行对象类的构造函数,同时该实例的属性和方法被this所引用,即this指向新构造的实例;
3. 如果构造函数return了一个新的‘对象’,那么这个对象就会取代真个new出来的结果,如果构造函数没有return对象,那么就会返回步骤1所创建的对象,即隐式返回this。(一般情况下构造函数不会返回任何值,不过在一些特殊情况下,如果用户想覆盖这个值,可以选择返回一个普通的对象来覆盖)

 

Guess you like

Origin blog.csdn.net/RedaTao/article/details/85118891