Home > Web Front-end > JS Tutorial > body text

JavaScript interview basic knowledge questions sharing

小云云
Release: 2020-07-30 17:03:35
Original
3266 people have browsed it

JavaScript interview basic knowledge questions sharing

According to a StackOverflow survey, JavaScript is the most popular programming language since 2014. Of course, this is reasonable, after all, 1/3 of development work requires some JavaScript knowledge. Therefore, if you want to become a developer, you should learn this language.

The main purpose of this blog is to summarize common concepts in all interviews so that you can quickly understand them. (In view of the fact that this article is too long to facilitate reading, it will be divided into three blogs for translation. This is the third part. For the first part, please click Quickly Master the Basics of JavaScript Interviews (1))

Recommended related articles:The most complete collection of js interview questions in 2020 (latest)

new keywords

If you use the new keyword to call a very special form of the function. We call those functions called with new constructor function.

What exactly does the function using new do?

  • Create a new object

  • Set the prototype of the object to the prototype of the constructor

  • Execute the constructor, thisExecute the newly constructed object

  • Return the object. If the constructor returns an object, then the constructed object is returned.

// 为了更好地理解底层,我们来定义new关键字
function myNew(constructor, ...arguments) {
  var obj = {}
  Object.setPrototypeOf(obj, constructor.prototype);
  return constructor.apply(obj, arguments) || obj
}
Copy after login

What is the difference between using new and not using it?

function Bird() {
  this.wings = 2;
}
/* 普通的函数调用 */
let fakeBird = Bird();
console.log(fakeBird);    // undefined
/* 使用new调用 */
let realBird= new Bird();
console.log(realBird)     // { wings: 2 }
Copy after login

In order to facilitate comparison and understanding, the translator has added an additional test situation:

function MBird(){
  this.wings =2; 
  return "hello";
}

let realMBrid = new MBird();
console.log(realMBird) // { wings: 2 }
Copy after login

You will find that this sentence return "hello" does not take effect!

Prototype and inheritance

Prototype (Prototype) is the most confusing concept in JavaScript. One of the reasons is that prototype can be used in two different situations.

  • Prototype relationship
    Every object has a prototype object, which contains all the properties of its prototype.
    .__proto__ is an informal mechanism (provided in ES6) used to obtain the prototype of an object. You can understand it as pointing to the parent of the object.
    All ordinary objects inherit the .constructor property, which points to the constructor of the object. When an object is implemented through a constructor, the __proto__ attribute points to the .prototype of the constructor's constructor. Object.getPrototypeOf() is a standard function of ES5, used to get the prototype of an object.

  • Prototype attributes
    Each function has a .prototype attribute, which contains all attributes that can be inherited. By default, this object contains the .constructor property pointing to the original constructor. Every object created using a constructor has a constructor property.

Let’s use examples to help understand:

function Dog(breed, name){
  this.breed = breed,
  this.name = name
}
Dog.prototype.describe = function() {
  console.log(`${this.name} is a ${this.breed}`)
}
const rusty = new Dog('Beagle', 'Rusty');

/* .prototype 属性包含了构造函数以及构造函数中在prototype上定义的属性。*/
console.log(Dog.prototype)  // { describe: ƒ , constructor: ƒ }

/* 使用Dog构造函数构造的对象 */
console.log(rusty)   //  { breed: "Beagle", name: "Rusty" }
/* 从构造函数的原型中继承下来的属性或函数 */
console.log(rusty.describe())   // "Rusty is a Beagle"
/* .__proto__ 属性指向构造函数的.prototype属性 */
console.log(rusty.__proto__)    // { describe: ƒ , constructor: ƒ }
/* .constructor 属性指向构造函数 */
console.log(rusty.constructor)  // ƒ Dog(breed, name) { ... }
Copy after login

The use of JavaScript can be said to be quite flexible. In order to avoid bugs and not knowing, you might as well connect to Fundebug Online real-time monitoring.

Prototype chain

The prototype chain refers to objects that are linked through prototypes to form a directed chain. When accessing a property of an object, the JavaScript engine will first check whether the object contains the property. If not, check to see if it is included in the object's prototype. And so on until the property is found or the last object is found. The prototype of the last object defaults to null.

Owning vs. Inheriting

An object has two properties, one defined by itself and one inherited.

function Car() { }
Car.prototype.wheels = 4;
Car.prototype.airbags = 1;

var myCar = new Car();
myCar.color = 'black';

/*  原型链中的属性也可以通过in来查看:  */
console.log('airbags' in myCar)  // true
console.log(myCar.wheels)        // 4
console.log(myCar.year)          // undefined

/*  通过hasOwnProperty来查看是否拥有该属性:  */
console.log(myCar.hasOwnProperty('airbags'))  // false — Inherited
console.log(myCar.hasOwnProperty('color'))    // true
Copy after login

Object.create(obj) Create a new object, prototype points to obj.

var dog = { legs: 4 };
var myDog = Object.create(dog);

console.log(myDog.hasOwnProperty('legs'))  // false
console.log(myDog.legs)                    // 4
console.log(myDog.__proto__ === dog)       // true
Copy after login

Inheritance is pass-by-reference.

Inherited attributes are all in the form of reference. Let's understand it vividly through examples:

var objProt = { text: 'original' };
var objAttachedToProt = Object.create(objProt);
console.log(objAttachedToProt.text)   // original

// 我们更改objProt的text属性,objAttachedToProt的text属性同样更改了
objProt.text = 'prototype property changed';
console.log(objAttachedToProt.text)   // prototype property changed

// 但是如果我们讲一个新的对象赋值给objProt,那么objAttachedToProt的text属性不受影响
objProt = { text: 'replacing property' };
console.log(objAttachedToProt.text)   // prototype property changed
Copy after login

Classic inheritance vs. Prototypal inheritance

Eric Elliott's article has a very detailed introduction: Master the JavaScript Interview: What's the Difference Between Class & Prototypal Inheritance?
The author believes that prototypal inheritance is superior to classic inheritance, and provides a video introduction: https://www.youtube.com/watch...

Asynchronous JavaScript

JavaScript It is a single-threaded programming language, which means that the JavaScript engine can only execute a certain piece of code at a time. The problem it causes is: if a piece of code takes a long time to execute, other operations will be stuck. JavaScript uses Call Stack to record function calls. A Call Stack can be viewed as a stack of books. The last book is placed on top and is removed first. The books placed first are on the bottom and are removed last.

In order to prevent complex code from occupying the CPU for too long, one solution is to define an asynchronous callback function. Let’s define an asynchronous function ourselves:

function greetingAsync(name, callback){
  let greeting = "hello, " + name ;
  setTimeout(_ => callback(greeting),0);
}

greetingAsync("fundebug", console.log);
console.log("start greeting");
Copy after login

我们在greetingAsync中构造了greeting语句,然后通过setTimeout定义了异步,callback函数,是为了让用户自己去定义greeting的具体方式。为方便起见,我们时候直接使用console.log
上面代码执行首先会打印start greeting,然后才是hello, fundebug。也就是说,greetingAsync的回调函数后执行。在网站开发中,和服务器交互的时候需要不断地发送各种请求,而一个页面可能有几十个请求。如果我们一个一个按照顺序来请求并等待结果,串行的执行会使得网页加载很慢。通过异步的方式,我们可以先发请求,然后在回调中处理请求结果,高效低并发处理。

下面通过一个例子来描述整个执行过程:

const first = function () {
  console.log('First message')
}
const second = function () {
  console.log('Second message')
}
const third = function() {
  console.log('Third message')
}

first();
setTimeout(second, 0);
third();

// 输出:
  // First message
  // Third message
  // Second message
Copy after login
  1. 初始状态下,浏览器控制台没有输出,并且事件管理器(Event Manager)是空的;

  2. first()被添加到调用栈

  3. console.log("First message")加到调用栈

  4. console.log("First message")执行并输出“First message”到控制台

  5. console.log("First message")从调用栈中移除

  6. first()从调用栈中移除

  7. setTimeout(second, 0)加到调用栈

  8. setTimeout(second, 0)执行,0ms之后,second()被加到回调队列

  9. setTimeout(second, 0)从调用栈中移除

  10. third()加到调用栈

  11. console.log("Third message")加到调用栈

  12. console.log("Third message")执行并输出“Third message”到控制台

  13. console.log("Third message")从调用栈中移除

  14. third()从调用栈中移除

  15. Event Loop 将second()从回调队列移到调用栈

  16. console.log("Second message")加到调用栈

  17. console.log("Second message")Second message”到控制台

  18. console.log("Second message")从调用栈中移除

  19. Second()从调用栈中移除

特别注意的是:second()函数在0ms之后并没有立即执行,你传入到setTimeout()函数的时间和second()延迟执行的时间并不一定直接相关。事件管理器等到setTimeout()设置的时间到期才会将其加入回调队列,而回调队列中它执行的时间和它在队列中的位置已经它前面的函数的执行时间有关。

相关学习推荐:javascript视频教程

The above is the detailed content of JavaScript interview basic knowledge questions sharing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template