Home > Web Front-end > JS Tutorial > 10 JavaScript interview questions frequently asked by developers (with answer analysis)

10 JavaScript interview questions frequently asked by developers (with answer analysis)

青灯夜游
Release: 2021-04-26 18:53:56
forward
3213 people have browsed it

This article introduces the 10 most frequently asked JavaScript interview questions for beginners. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to beginners.

10 JavaScript interview questions frequently asked by developers (with answer analysis)

In this article, I have collected the 10 most frequently asked questions about Javascript and their answers.

Most of these 10 questions involve the basics of Javascript, so if you are just starting to learn JS, it is best to understand and master them.

This 10 question involves closures, promise, variable promotion, classes, etc. in JS. While these aren't very difficult to learn, it's good to know the answers because some of them are often asked in interviews. [Recommended learning: javascript advanced tutorial]

What is the closure in Javascript?

A closure is a combination of functions enclosed together, where the inner function can access its variables and the variables of the outer function.

The simplest way to explain it is the above example:

function outer() {
  var name = 'Maria';
  function inner() {
    console.log(name);
  }
  inner();
}
outer();
// 'Maria'
Copy after login

In the above code, you can see that the inner() function can access its parent function variablename. Therefore, if the outer() function is called, then the inner() function's console.log() will return the value of name ##Maria.

Internal functions can access external function parameter objects, but internal function parameters are the same as external ones, so the internal parameter object will overwrite the external parameter object. It looks like this:

function outer(a, b) {
  const inner = (a, b) => console.log(a, b);
  inner(1, 2);
}
outer('Alice', 'Mark');
// returns 1, 2
Copy after login

The main reason why we use closures is to return functions that can return other functions.

What is DOM in Javascript

DOM is the Document Object Model, which is an object-oriented representation of the website and can be modified using Javascript.

Use JS to manipulate DOM elements, such as color, position, and size. To select specific elements of the page, Javascript provides some methods:

  • getElementById() - Select an element by id attribute
  • getElementsByName() - Select an element via the name attribute
  • getElementsByTagName() - Select all elements of the selected tag,
  • getElementsbyClassName() - Select all elements with a specific class name
* 

querySelector() - Select elements through CSS selectors.

Javascript also provides other methods of operating elements, not just getting elements, such as

appendChild() or innerHTML().

What is Promise in Javascript

Promise is a solution for asynchronous programming that can replace traditional solutions-callback functions and events. ES6 unifies usage and provides

Promise objects natively. As an object, Promise has the following two characteristics: (1) The state of the object is not affected by the outside world. (2) Once the state changes, it will not change. That is to say, Promise has only one state at any time.

Promise has three states, namely:

Pending (in progress), Resolved (completed), Rejected (failed). Promise starts from the Pending state. If it succeeds, it goes to the success state and executes the resolve callback function; if it fails, it goes to the failure state and executes the reject callback function. .

If the Promise is resolved, we can call the

then() method and use the return value to perform the operation. If rejected, we can use the catch() method to handle the error.

Other ways to handle asynchronous programming are

async/await and callbacks.

What is the prototype in Javascript?

Prototype usually refers to the two prototype objects

prototype and __proto__ , the former is called an explicit prototype object, and the latter is called an implicit prototype object.

Javascript objects inherit methods and properties from the prototype, and

Object.prototype is at the top of the inheritance chain. The Javascript prototype keyword can also be used to add new values ​​and methods to the constructor.

Let’s take a look at the example:

function Animal(name, kind, age) {
  this.name = name;
  this.kind = kind;
  this.age = age;
}

Animal.prototype.ownerName('Mark');
Copy after login

As you can see, by using the prototype, we can add the

ownerName attribute to the Animal() constructor middle.

What is variable promotion in Javascript

Promotion is a mechanism that promotes all declared variables and functions to their local scope If variables and functions are placed in the global scope, they will be promoted to the top of the global scope.

In Javascript, you can declare a variable after it is used.

Promotion is used to avoid undefined errors caused by executing a variable or function before it is defined.

name = 'Ted';
console.log(name);
var name;
// 'Ted'


var name;
name = 'Ted';
console.log(name);
// 'Ted';
Copy after login

使用 var 声明的变量,如果没有赋值,则默认会被初始化为 undefined, letconst 则不会。另外,需要注意的是,在声明const时,必须同时初始化它,因为后面不可在更改它。

Javascript中的对象是什么

对象只是一种特殊的数据。对象拥有属性和方法。JavaScript 中的所有事物都是对象,如:字符串、数值、数组、函数等。

对象的属性:反映该对象某些特定的性质的,如:字符串的长度、图像的长宽等;

对象的方法:能够在对象上执行的动作。例如,表单的“提交”(Submit),时间的“获取”(getYear)等;

属性只是简单的值,而方法是可以在对象上执行的操作。

var student = {
  firstName: 'Alice',
  lastName: 'Jones',
  age: 21,
  sayHi: () => {
    return 'Hi, I am ' + this.firstName;
  }
}
Copy after login

在上面的代码中,你可以看到Student对象,其中包含三个属性和一个方法。

Javascript 中的函数是什么

在javascript中函数是一段可以被执行或调用任意次数的JavasScript代码,在数据类型中属于"function"。函数也拥有属性和方法,因此函数也是对象。

在Javascript中函数定义函数声明或函数表达式由关键字function开始。在定义函数时,可以在函数名后面的括号中添加一些参数。当我们调用函数时,括号中传递的值称为参数。

function calculate(x, y) {
  return x * y;
}

calculate(2, 5);
Copy after login

Javascript中的纯函数是什么

如果函数的调用参数相同,则永远返回相同的结果。它不依赖于程序执行期间函数外部任何状态或数据的变化,必须只依赖于其输入参数。

顾名思义,纯函数跟我们初中数学的基本函数一样,遵循一定的映射关系,输入决定输出,一个输入只能对应一个输出。不同的输入可以有相同的输出,但是相同的输入不能有不同的输出

一个函数,如果符合以下两个特点,那么它就可以称之为 纯函数:

  • 对于相同的输入,永远得到相同的输出
  • 没有任何可观察到的副作用

Javascript中的构造函数是什么

构造函数是一种特殊的方法,用于初始化和创建 Javascript 中的对象。

JavaScript 中的构造函数和其它语言中的构造函数是不同的。 通过 new 关键字方式调用的函数都被认为是构造函数。

在构造函数内部,this 指向新创建的对象 Object。 这个新创建的对象的 prototype 被指向到构造函数的 prototype

如果被调用的函数没有显式的 return 表达式,则隐式的会返回 this 对象,也就是新创建的对象。

const Person = (name, age) => {
  this.name = name;
  this.age = age;
}

var man = new Person('Mark', 23);
console.log(man);
// { name: 'Mark', age: 23 }
Copy after login

在上面的代码中,我创建了一个Person构造函数,在下面的代码中,创建了一个名为man的新变量,并基于Person构造函数创建了一个新对象。

Javascript类是什么?

自从 ES6 引入以来,我们可以在Javascript中使用类。 类是一种函数,我们使用关键字class代替function 关键字来初始化它。

除此之外,我们还必须在类内部添加constructor()方法,该方法在每次初始化类时都会调用。

constructor()方法内部,我们添加了类的属性。 要基于现有的类创建另一个类,我们可以使用extends关键字。

在JavaScript中使用类的一个很好的例子是 React 框架,它是类的组件。

总结

在本文中,我收集了开发者经常问的 10 个Javascript问题,并给出答案,答案不是唯一,这里只是自己的一些见解,希望本文能给初学者带来一些帮助。

原文地址:https://dev.to/duomly/10-most-common-javascript-questions-answered-1083

作者:Duomly

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of 10 JavaScript interview questions frequently asked by developers (with answer analysis). For more information, please follow other related articles on the PHP Chinese website!

source:segmentfault.com
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