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,
this
Execute the newly constructed objectReturn 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 }
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 }
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 }
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 aprototype
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 theparent
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) { ... }
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
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
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
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");
我们在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
初始状态下,浏览器控制台没有输出,并且事件管理器(Event Manager)是空的;
first()
被添加到调用栈将
console.log("First message")
加到调用栈console.log("First message")
执行并输出“First message”到控制台console.log("First message")
从调用栈中移除first()
从调用栈中移除setTimeout(second, 0)
加到调用栈setTimeout(second, 0)
执行,0ms之后,second()
被加到回调队列setTimeout(second, 0)
从调用栈中移除third()
加到调用栈console.log("Third message")
加到调用栈console.log("Third message")
执行并输出“Third message”到控制台console.log("Third message")
从调用栈中移除third()
从调用栈中移除Event Loop 将
second()
从回调队列移到调用栈console.log("Second message")
加到调用栈console.log("Second message")
Second message”到控制台console.log("Second message")
从调用栈中移除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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The Go framework is a set of components that extend Go's built-in libraries, providing pre-built functionality (such as web development and database operations). Popular Go frameworks include Gin (web development), GORM (database operations), and RESTful (API management). Middleware is an interceptor pattern in the HTTP request processing chain and is used to add functionality such as authentication or request logging without modifying the handler. Session management maintains session status by storing user data. You can use gorilla/sessions to manage sessions.

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity
