Home>Article>Web Front-end> 10 JavaScript interview questions frequently asked by developers (with answer analysis)
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.
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 involvesclosures,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]
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'
In the above code, you can see that theinner()
function can access its parent function variablename
. Therefore, if theouter()
function is called, then theinner()
function'sconsole.log()
will return the value ofname
##Maria.
function outer(a, b) { const inner = (a, b) => console.log(a, b); inner(1, 2); } outer('Alice', 'Mark'); // returns 1, 2The main reason why we use closures is to return functions that can return other functions.
attribute
attribute
querySelector()- Select elements through CSS selectors.
Javascript also provides other methods of operating elements, not just getting elements, such asappendChild()or
innerHTML().
Promiseobjects 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,Promisehas only one state at any time.
Pending(in progress),Resolved(completed),Rejected(failed). Promise starts from thePendingstate. If it succeeds, it goes to the success state and executes the
resolvecallback function; if it fails, it goes to the failure state and executes the
rejectcallback function. .
then()method and use the return value to perform the operation. If rejected, we can use the
catch()method to handle the error.
async/awaitand
callbacks.
prototypeand
__proto__, the former is called an explicit prototype object, and the latter is called an implicit prototype object.
Object.prototypeis at the top of the inheritance chain. The Javascript prototype keyword can also be used to add new values and methods to the constructor.
function Animal(name, kind, age) { this.name = name; this.kind = kind; this.age = age; } Animal.prototype.ownerName('Mark');As you can see, by using the prototype, we can add the
ownerNameattribute to the
Animal()constructor middle.
Promotionis 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.Promotionis used to avoidundefinederrors 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';
使用var
声明的变量,如果没有赋值,则默认会被初始化为undefined
,let
和const
则不会。另外,需要注意的是,在声明const
时,必须同时初始化它,因为后面不可在更改它。
对象只是一种特殊的数据。对象拥有属性和方法。JavaScript 中的所有事物都是对象,如:字符串、数值、数组、函数等。
对象的属性:反映该对象某些特定的性质的,如:字符串的长度、图像的长宽等;
对象的方法:能够在对象上执行的动作。例如,表单的“提交”(Submit),时间的“获取”(getYear)等;
属性只是简单的值,而方法是可以在对象上执行的操作。
var student = { firstName: 'Alice', lastName: 'Jones', age: 21, sayHi: () => { return 'Hi, I am ' + this.firstName; } }
在上面的代码中,你可以看到Student
对象,其中包含三个属性和一个方法。
在javascript中函数是一段可以被执行或调用任意次数的JavasScript代码,在数据类型中属于"function"
。函数也拥有属性和方法,因此函数也是对象。
在Javascript中函数定义函数声明或函数表达式由关键字function
开始。在定义函数时,可以在函数名后面的括号中添加一些参数。当我们调用函数时,括号中传递的值称为参数。
function calculate(x, y) { return x * y; } calculate(2, 5);
如果函数的调用参数相同,则永远返回相同的结果。它不依赖于程序执行期间函数外部任何状态或数据的变化,必须只依赖于其输入参数。
顾名思义,纯函数跟我们初中数学的基本函数一样,遵循一定的映射关系,输入决定输出,一个输入只能对应一个输出。不同的输入可以有相同的输出,但是相同的输入不能有不同的输出
一个函数,如果符合以下两个特点,那么它就可以称之为 纯函数:
构造函数是一种特殊的方法,用于初始化和创建 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 }
在上面的代码中,我创建了一个Person
构造函数,在下面的代码中,创建了一个名为man
的新变量,并基于Person
构造函数创建了一个新对象。
自从 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!