Which of the most error-prone JavaScript interviews did you recruit?

1 Introduction

During this time, Jin Sanyin, a lot of people interviewed, many people share interview questions. Some time ago, I also temporarily served as an interviewer. In order to understand the interviewer's level, I also wrote a topic and interviewed several front-end developers. During this period of time, I was learning, some knowledge of writing design patterns, and unexpected knowledge of design patterns, which were frequent test points in interview questions. So, sum up today, those test sites that make people fall pit.

2. Object-oriented programming

Regarding object-oriented and process-oriented, individuals feel that the two are not absolutely independent, but are mutually compatible. As for when to use object-oriented and when to use process-oriented, specific analysis.

For object-oriented programming. Knowing that there is a high praise response:

Object-oriented: dog. Eat (屎)

Process-oriented: Eat. (Dog, 屎)

What are the most error-prone points for JavaScript interviews?

But this example does not feel very elegant, I changed it, give an elegant little example to illustrate the difference between object-oriented and process-oriented.

Demand: Define 'waiting for hot pot'

The object-oriented thinking is: waiting. Action (eat hot pot)

The process-oriented thinking is: action (watching, eating hot pot)

Code implementation:

What are the most error-prone points for JavaScript interviews?

The results are the same, are output 'waiting to eat hot pot'. But if I'm full now, I'm ready to write the code. How can this be achieved? Look at the code

The result is the same: 'wait and write code'

However, it is not difficult to find that object orientation is more flexible, more reusable, and more scalable. Because object-oriented is to perform certain actions on the object (in the example: 'waiting'). These actions can be custom extended.

The process is to define a lot of actions to specify who will perform this action.

Well, the simple object-oriented description is here, as for the three major features of object-oriented: inheritance, encapsulation, polymorphism this online search for information.

3.this

When developing with JavaScript, many developers are more or less targeted by this, but in fact, the most important thing to remember about the pointing of this is: which object calls the function and which object the function points to. .

What are the most error-prone points for JavaScript interviews?

The following is divided into several situations to talk about

3-1. Ordinary function call

This situation is not particularly unexpected, that is to point to the global object -window.

What are the most error-prone points for JavaScript interviews?

Maybe everyone will be confused, why is not the output waiting, but in a closer look, I declare the way is let, will not be the window object

If the output is waiting, write it like this

What are the most error-prone points for JavaScript interviews?

3-2. Object Function Calls

This is not difficult to understand, it is the function call, this point where

What are the most error-prone points for JavaScript interviews?

Obviously, the first time it outputs obj.a, it is 111. For the second time, obj does not have b attribute, so it outputs undefined because this points to obj.

But the following situation must be noted

What are the most error-prone points for JavaScript interviews?

This belief is not difficult to understand, although obj1.fn is assigned from obj2.fn, but the calling function is obj1, so this points to obj1.

3-3. Constructor calls

What are the most error-prone points for JavaScript interviews?

This is also not difficult to understand, the memory (new four steps) is almost!

However, there is a pit, although it does not usually appear, but it is necessary to mention it.

Returning an object inside a constructor returns the object directly instead of the object created after the constructor is executed

3-4.apply and call calls

Apply and call simply change this to the passed function.

What are the most error-prone points for JavaScript interviews?

At this point, although obj2 calls the method, it uses a call and dynamically points this to obj1. Equivalent to this obj2.fn this execution environment is obj1. The details of apply and call are mentioned below.

3-5. Arrow function call

First of all, I have to say that ES6 provides arrow functions, which increase our development efficiency. However, in the arrow function, there is no this. The arrow inside this function inherits the environment.

one example

It is not difficult to find that although this in fn() is pointing to obj, the function passed to setTImeout is an ordinary function, this is pointed to window, and window has no a below, so the output is undefined.

Replace arrow functions

What are the most error-prone points for JavaScript interviews?

The output 222 this time is because the arrow function is passed to setTImeout. Then there is no this in the arrow function, so we need to find the upper scope. In this example, the upper scope of setTImeout is fn. The this inside fn points to obj, so the arrow function in setTImeout inside this points to obj. So output 222.

4.call and apply

The effect of call and apply is exactly the same. The only difference is the parameter.

The parameters received by call are not fixed, the first parameter is the point of this in the body of the function, and the second parameter is the parameter passed in turn.

Apply receives two arguments. The first argument is also the pointer to the this in the body of the function. The second parameter is a collection object (array or array of classes)

What are the most error-prone points for JavaScript interviews?

Like the above example

What are the most error-prone points for JavaScript interviews?

The two main uses of call and apply are

1. Change the pointing of this (point this from obj2 to obj1)

2. Method borrowing (obj1 does not have fn, just borrows obj2 methods)

5. Closure

Closure this may be confused, but the concept must be conquered! Here's a simple example

What are the most error-prone points for JavaScript interviews?

Then execute it several times!

The result of the above figure shows that the variable now does not get recycled after the execution of the function, but continues to be stored in memory.

The specific reason is as follows: Just started in, because it is an automatic execution function, it will be automatically executed at the beginning, this one

Then assign this object to add. Since there is a function in add, it depends on the now variable. So now it will not be destroyed and recycled. This is one of the uses of closures (continuation of variable cycles). Since now is not accessible outside, this is another use of closures (creating local variables, protecting local variables from being accessed and modified).

Someone may have doubts that closures can cause memory leaks. But everybody wants to, the above example, if you don't use closures, use global variables. Put variables in the closure and in the global variables, the effect is the same. Using closures can also reduce global variables, so the above example closure is better!

6. Summary

When I was learning the design patterns, I encountered some of these knowledge points. These knowledge points are also the places where I have more pits in the group chat and community. This knowledge can be said to be commonly used for development, knowledge of interviews, or suggestions for further study. There is also a simple over there. Not deep. If you have any suggestions on the article, welcome to give advice.

Power Terminal Block

The terminal is used to facilitate the connection of wires. It is actually a piece of metal enclosed in insulating plastic. There are holes at both ends to insert wires. There are screws for fastening or loosening, such as two wires, sometimes Need to connect, sometimes need to be disconnected, then you can use the terminal to connect them, and can be disconnected at any time, without having to solder or twist them together, very convenient and fast.


Power Terminal Block,High Power Terminal Connector Block,Power Terminal Block Connector,Ac Power Terminal Block

Sichuan Xinlian electronic science and technology Company , https://www.sztmlchs.com

This entry was posted in on