In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you how to answer these 10 most common Javascript questions, the content is very detailed, interested friends can refer to, hope to be helpful to you.
What is the closure in Javascript?
A closure is a combination of closed functions where internal functions can access their variables and variables of external functions.
The simplest way to explain is the example above:
Function outer () {var name = 'Maria'; function inner () {console.log (name);} inner ();} outer (); / /' Maria'
In the above code, you can see that the inner () function can access its parent function variable name. Therefore, if the outer () function is called, the console.log () of the inner () function returns the value Maria of name.
The internal function can access the external function parameter object, but the internal function parameter object is the same as the external function parameter object, then the internal parameter object overrides the external parameter object. As follows:
Function outer (a, b) {const inner = (a, b) = > console.log (a, b); inner (1,2);} outer ('Alice',' Mark'); / / returns 1,2
The main reason 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 a Web site and can be modified using Javascript.
Use JS to manipulate DOM elements, such as color, position, and size. To select specific elements of a page, Javascript provides several methods:
GetElementById ()-Select an element through the id attribute
GetElementsByName ()-Select an element through the name attribute
GetElementsByTagName ()-Select all elements of the selected tag
GetElementsbyClassName ()-Select all elements of a specific class name
* querySelector ()-Select the element through the CSS selector.
Javascript also provides other ways to manipulate elements, rather than just getting elements, such as appendChild () or innerHTML ().
What is Javascript's Promise?
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, there is only one state for Promise at any time.
Promise has three states: * * Pending * * (in progress), * * Resolved (completed), and Rejected * * (failed). Promise starts in the Pending state and, if successful, goes to the successful state and executes the resolve callback function; if it fails, it goes to the failed state and executes the reject callback function.
If the Promise is parsed (resolved), we can call the then () method and perform the operation with the return value. If rejected (rejected), we can use the catch () method to handle the error.
Other ways to deal with asynchronous programming are async/await and callbacks.
What is the prototype in Javascript?
Stereotype usually refers to two prototype objects, prototype and _ _ proto__, in which the former is called explicit prototype object and the latter is called implicit prototype object.
The Javascript object inherits methods and properties from the prototype, while 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 examples:
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 ownerName attribute to the Animal () constructor.
What is the variable promotion of Javascript
Promotion is a mechanism that promotes all declared variables and functions to the top of their local scope, and if variables and functions are placed in the global scope, they are promoted to the top of the global scope.
In Javascript, you can declare a variable after it has been used.
Promotion is used to avoid undefined errors caused by execution of a variable or function before it is defined.
Name = 'Ted'; console.log (name); var name; / /' Ted' var name; name = 'Ted'; console.log (name); / /' Ted'
Variables declared with var, if not assigned, are initialized to undefined by default, while let and const are not. In addition, it is important to note that when you declare a const, you must initialize it at the same time, because it cannot be changed later.
What are the objects in Javascript?
An object is just a special kind of data. Objects have properties and methods. Everything in JavaScript is an object, such as strings, numeric values, arrays, functions, and so on.
Properties of an object: reflecting some specific properties of the object, such as the length of the string, the length and width of the image, etc.
Method of an object: an action that can be performed on an object. For example, "Submit" of the form, "getYear" of time, etc.
Properties are simple values, and methods are actions that can be performed on an object.
Var student = {firstName: 'Alice', lastName:' Jones', age: 21, sayHi: () = > {return'Hi, I am'+ this.firstName;}}
In the above code, you can see the Student object, which contains three properties and a method.
What is the function in Javascript
In javascript, a function is a piece of JavasScript code that can be executed or called any number of times, and belongs to "function" in the data type. Functions also have properties and methods, so functions are also objects.
In Javascript, a function definition function declaration or function expression starts with the keyword function. When defining a function, you can add some parameters in parentheses after the function name. When we call a function, the value passed in parentheses is called a parameter.
Function calculate (x, y) {return x * y;} calculate (2,5)
What is the pure function in Javascript
If the calling parameters of the function are the same, the same result is always returned. It does not depend on any changes in state or data outside the function during program execution, but must only rely on its input parameters.
As the name implies, the pure function, like the basic function of our junior high school mathematics, follows a certain mapping relationship, input determines output, and one input can only correspond to one output. Different inputs can have the same output, but the same input cannot have different outputs.
A function can be called a pure function if it meets the following two characteristics:
For the same input, always get the same output
There are no observable side effects.
What is the constructor in Javascript
The constructor is a special method for initializing and creating objects in the Javascript class.
Constructors in JavaScript are different from constructors in other languages. Functions called through the new keyword are considered constructors.
Inside the constructor, this points to the newly created object Object. The prototype of the newly created object is pointed to the prototype of the constructor.
If the called function does not have an explicit return expression, it implicitly returns a this object, that is, the newly created object.
Const Person = (name, age) = > {this.name = name; this.age = age;} var man = new Person ('Mark', 23); console.log (man); / / {name:' Mark', age: 23}
In the above code, I created a Person constructor, and in the following code, I created a new variable named man and a new object based on the Person constructor.
What is the Javascript class?
Since the introduction of ES6, we can use classes in Javascript. Class is a function, and we initialize it with the keyword class instead of the function keyword.
In addition, we must add the constructor () method inside the class, which is called every time the class is initialized.
Inside the constructor () method, we added properties of the class. To create another class based on an existing class, we can use the extends keyword.
A good example of using a class in JavaScript is the React framework, which is a component of the class.
On how to answer these 10 most common Javascript questions to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.