This is the sixth day of my participation in the August Text Challenge.More challenges in August

Related articles:

  • 5 JavaScript interview questions you can use to interview potential candidates
  • 5 JavaScript interview questions you can use to interview potential candidates
  • 5 JavaScript interview questions you can use to interview potential candidates
  • 5 JavaScript interview questions you can use to interview potential candidates

1. Explain how prototypal inheritance works.

This is a very common JavaScript problem. All JS objects have a __proto__ attribute that points to their protoobject. When it tries to access an object’s properties, if it doesn’t find one on that object, it also searches for that object’s prototype, and the prototype of that object’s prototype, working its way up until it finds an attribute with a matching name or reaches the end of the prototype chain. This behavior mimics classical inheritance, but more delegation than inheritance.

2. Tell me what you know about AMD and CommonJS.

They are all ways to implement a modular architecture, which JavaScript didn’t have until ES2015. CommonJS is synchronous, while AMD (Asynchronous Module Definition) is obviously Asynchronous. CommonJS is designed with server-side development in mind, while AMD supports asynchronous loading modules that are better suited for browsers.

AMD’s syntax is very verbose, and CommonJS is closer to the usage conventions of import statements in other languages. For the most part, I don’t think AMD needs to use it because if you bundle all your JavaScript into one file, you won’t get the benefits of asynchronous loading. In addition, CommonJS is syntactically closer to Node’s modular writing style, with less context switching overhead when switching between both ends of development using JavaScript.

It’s nice to see that ES2015 supports both synchronous and asynchronous module loading solutions, so we can finally use only one solution. It’s not yet fully available in browsers and Node, but you can use transcoding tools to do the conversion.

3. null,undefinedWhat is the difference between undeclared variables and undeclared variables? How are these state values checked and determined?

Variables are undeclared variables when you assign a value to a variable without declaring it first using var, let, or const. Undeclared variables leave the current scope and become variables defined under the global scope. In strict mode, assigning a value to an undeclared variable raises ReferenceError. Like using global variables, using undeclared variables is a very bad practice and should be avoided whenever possible. To check for them, place the code that uses them in a try/catch statement.

function foo() {
    x = 1; // In strict mode, ReferenceError is thrown
}

foo();
console.log(x); / / 1
Copy the code

The value of a variable is undefined when it has been declared but not assigned. If the result of a function is assigned to a variable, but the function returns no value, the value of the variable is undefined. To check it, use strict equality (===); Or use typeof, which returns the string ‘undefined’. Note that non-strict equality (==) cannot be used for checking, because it will also return true if the variable value is null.

var foo;
console.log(foo); // undefined
console.log(foo === undefined); // true
console.log(typeof foo === 'undefined'); // true

console.log(foo == null); // true. Error, do not use non-strict equality!

function bar() {}
var baz = bar();
console.log(baz); // undefined
Copy the code

Null can only be explicitly assigned to a variable. It represents a null value, as opposed to the meaning of being explicitly assigned to undefined. To check for null values, use the strict equality operator. Note that, as before, non-strict equality (==) cannot be used to check, because it will return true if the variable value is undefined.

var foo = null;
console.log(foo === null); // true
console.log(foo == undefined); // true. Error, do not use non-strict equality!
Copy the code

As a matter of personal habit, I never use undeclared variables. If I define variables that are not used for the time being, I explicitly assign them null after the declaration.

4. .calland.applyWhat’s the difference?

Both.call and.apply are used to call functions, and the first argument will be used as the value of this within the function. However,.call accepts a comma-separated argument as the following argument, while.apply accepts an array of arguments as the following argument. An easy way to remember this is to associate the C in call with comma-separated and the A in Apply with array.

function add(a, b) {
  return a + b;
}

console.log(add.call(null.1.2)); / / 3
console.log(add.apply(null[1.2])); / / 3
Copy the code

5. Please specifyFunction.prototype.bindThe use of.

From MDN:

The bind() method creates a new function that, when called, sets its this keyword to the supplied value and, when called, provides a given sequence of arguments before any supply.

It is useful to bind the value of this to a method of a class that you want to pass to another function. This is often done in the React component.