Front end JS Face question summary Part 3 (host object and native object/function call mode/call and Apply/bind/document.write)

Source: Internet
Author: User

Original: Https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questions.md

We will continue to translate JavaScript interview questions in the near past and hope to help you.

(The italic part of the article is added to the translator)

Directory:

Part 1 (Event delegate/this keyword/prototype chain/AMD and commonjs/self-executing function)

Part 2 (NULL and undefined/closure/foreach with map/anonymous function/code organization)

Part 3 (Host object and native object/function call mode/call and Apply/bind/document.write)

1. What is the difference between the host object and the native object?

Native objects are objects that are defined in JavaScript in accordance with the ECMAScript specification, such as: String,math,regexp,object,function, and so on.

A host object is an object that is provided by the JavaScript runtime (browser or node), such as Window,xmlhttprequest, and so on.

Reference Document:

Https://stackoverflow.com/questions/7614317/what-is-the-difference-between-native-objects-and-host-objects

2. How the following function calls are different: function person () {},var person = person (), and var person = new Person ()

The question is rather confusing. I guess the problem is mainly to test the constructor in JavaScript. Technically, the function person () {} is just a normal declaration of functions. Usually we use the Pascal nomenclature to name the constructor.

var person = person () This simply calls the person as a function, not a constructor. It is a very common mistake to call a function as a constructor in this way. In general, the constructor does not have any return values. Therefore, if a constructor is called as a normal function and the return value is assigned to a variable, then the value will be undefined.

var person = new Person () creates an instance of a person object with the new operator and inherits the Person.prototype property. You can also create instances using object.create, such as Obejct.create (Person.prototype).

1 functionPerson (name) {2    This. Name =name;3 }4 5 varperson = person (' John ');6Console.log (person);//undefined7Console.log (Person.name);//uncaught typeerror:cannot Read Property ' name ' of undefined8 9 varperson =NewPerson (' John ');TenConsole.log (person);//Person {name: "John"} OneConsole.log (Person.name);//"John"

Reference Document:

Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

  

3. What is the difference between call and apply?

Both call and apply can be used to invoke a function, and the first passed argument is the inside of the function this object. The following parameters of the call method are delimited by commas, and apply is an array of uses. Shorthand method: C is the call method comma (comma) delimited, A is an apply array (arrays) delimited.

  

1 function Add (A, b) {2   return A + b; 3 }45 console.log (Add.call (null/  36 Console.log (add.apply (null//  3

4. What is the role of Function.prototype.bind?

The original interpretation of the quoted MDN:

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

In my experience, its biggest function is to pass a function as an argument to another function, which can bind the this value in the method. The most common is the components in the react.

Reference Document: Https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

  

5. When to use document.write ()

document.write () writes a string literal to the Document.open () open document stream. If the document is already loaded when document.write () is called, then he will call the Document.open method to clear how the document (

There are also other uses of document.write on the web, such as code analysis, or the introduction of styles in an environment that can only be used with JavaScript. It is also often used in HTML5 template files to load script files in parallel, maintaining the order in which code is executed. But I still disagree with these points, because in today's modern day, we have more and better ways to replace document.write ().

Reference Document:

Https://www.quirksmode.org/blog/archives/2005/06/three_javascrip_1.html

Https://github.com/h5bp/html5-boilerplate/wiki/Script-Loading-Techniques#documentwrite-script-tag

Front end JS Face question summary Part 3 (host object and native object/function call mode/call and Apply/bind/document.write)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.