preface

In this article, I recorded that when I interviewed for a node.js backend development position, the second interviewer asked me about the knowledge of JavaScript prototype chain and hand-wrote the code 😱 in person.

I hope this article will be helpful for those of you who are looking for back-end development jobs for the web front-end, Node.js, this year.

Essential concept

Prototype: Whenever a new function is created, a Prototype property is created for that function according to a specific rule. Only functions have prototype properties.

Proto: All objects contain a __proto__ attribute pointing to the prototype of their constructor.

Constructor: All objects contain the constructor property, which contains a pointer to the function where the Prototype property is located.

scenes

The day of the second interview was raining heavily 🌧, and the time of the interview was about 9:30 in the morning. Half wet, I was led to the conference room by HR, and soon a man of about 40 came in with several pieces of white paper in his hand.

After I went through the requisite protocol, he made a brief introduction (to the department manager).

.

Do you have a pen? He asked.

I said: Yes.

He said: “Here are a few pieces of paper, you will need to write code, easy, let’s start ~

Clothes wet too much, it was really a little cold 🥶

Simulate the implementation of the new operator

Call the constructor using the new operator

  1. Create a new object;
  2. Assign the scope of the constructor to the new object;
  3. Execute the code in the constructor;
  4. Returns a new object;
 functionanalog_new(constructor, ... rest) {if(typeof constructor ! = ='function') {
        returnconstructor; } const _constructor = object.create (constructor. Prototype); Const obj = constructor. Apply (_constructor, rest); // Return the result of the constructor execution if the result is an objectif (typeof obj === 'object') {
        return obj;
    } else {
        return_constructor; }};Copy the code

What is inheritance

Inheritance, encapsulation and polymorphism are the basic characteristics of object-oriented programming. By inheriting subclasses, you can not only have the methods and attributes of the parent class but also add new attributes and methods or modify the attributes and methods of the parent class.

It’s official: Inheritance is a fundamental feature of object-oriented programming. With inheritance, you can define subclasses that reuse (inherit), extend, or modify the behavior of the parent class. Classes whose members are inherited are called base classes. A class that inherits a member of a base class is called a derived class.

How does JS implement inheritance

Inheritance can be achieved through a chain of archetypes.

Js implementation inheritance in what ways

  1. Borrowing constructor
  2. Prototype chain
  3. Combination of inheritance

Please write out

I didn’t ask about the shortcomings of the constructor implementation inheritance and prototype chain inheritance, but I added them when I wrote the article.

Borrow constructors for inheritance

 function Animal(type) {
    this.type = type;
 }

 function Duck() {// Inherit Animal Animal. Call (this,'duck'); } const duck = new Duck(); console.log(duck.type); //duckCopy the code

Disadvantages: Cannot inherit attributes from the Animal prototype chain

 function Animal(type) {
    this.type = type;
 }

 Animal.prototype.walk = function (){
    console.log('walk');
 }

 function Duck() {// Inherit Animal Animal. Call (this,'duck'); } const duck = new Duck(); console.log(duck.type); //duck duck.walk(); //Error:duck.walk is not afunction
Copy the code

Prototype chain inheritance

 function Animal(type) {
     this.type = type;
 }
 Animal.prototype.walk = function () {
     console.log('walk... ');
 }

 function Duck() { }
 Duck.prototype = new Animal('duck'); // Assign Duck to Animal const Duck = new Duck(); console.log(duck.type); //duck duck.walk(); //walk...Copy the code

Disadvantages: When an instance modifies an attribute on the stereotype chain, if the instance type is a reference type, the attributes of other instances will be modified as well.

 function Animal(type) {
    this.type = type;
    this.colors = ['yellow'];
 }
 Animal.prototype.walk = function () {
    console.log('walk... ');
 }

 function Duck() { }
 Duck.prototype = new Animal('duck'); // Assign Duck to Animal const normal_duck = new Duck(); Const variation_duck = new Duck(); // Duck variation_duck.colors. Push ('black'); Normal_duck console.log(normal_duck.colors); // Change the color of the variation_duck object and the color of the normal_duck object. / / /'yellow'.'black'] console.log(variation_duck.colors); / / /'yellow'.'black' ]
Copy the code

Combination of inheritance

 function Animal(type) {
    this.type = type;
    this.color = ['yellow'];
 }
 Animal.prototype.walk = function () {
    console.log('walk... ');
 }

 function Duck() {
    Animal.call(this, 'duck'); } Duck.prototype = Object.create(Animal.prototype); Duck.prototype.constructor = Duck; const normal_duck = new Duck(); Const variation_duck = new Duck(); // Duck variation_duck.color.push('black'); Normal_duck console.log(normal_duck.color); // Change the color attribute of the variation_duck object. / / /'yellow'] console.log(variation_duck.color); / / /'yellow'.'black' ]
Copy the code

conclusion

To be honest, I was a little nervous about writing code in person for the first time. This kind of circumstance should be less! I’m going to write some other stuff that doesn’t have to do with the prototype chain, but I’m not going to write it here. But in the end, there are more interactions like this: the interviewer asks you to write a piece of code by hand, and then asks questions over and over again…

After nearly two hours of fierce competition, HR finally talked with me about my salary.

Here is my personal experience:

  1. Under the premise of ensuring the speed of the answer, we must pay attention to the neat handwriting.
  2. Code naming must be standard, try to write/say what you know.
  3. Use both hands to hand the paper to the interviewer during the communication.

Finally, you can find your favorite job in 2020. ❤ ❤ ❤

reference

<

> C# inheritance

My column

Getting started with Node.js

Learn node.js asynchronous programming

Node.js Back-end development series sequelize zero-based quickstart