JavaScript Error Record variable definition elevation, this pointer pointing, operator precedence, prototype, inheritance, global variable pollution, object properties, and prototype attribute precedence

Source: Internet
Author: User

Original location http://caibaojian.com/toutiao/5446

1. All variable declarations (VAR) or declared functions will be promoted to the top of the current function

With regard to function expressions, JS splits the code into two lines of code to execute separately. It's important to note that

var getName and function GetName are declaration statements, except that Var getName is a function expression , function getName is a declaration of functions , In this case, the function declaration and the variable declaration are the same as when the Declaration is advanced to the top of the current function, and the second function declaration promotes the function declaration to the top of the current function. (This is a bit around, but you can see that the essence is the declaration in advance, function getname is the functions of the Declaration, Declaration in advance, and Var getname followed by a function expression, and declared variables, declaration in advance, the assignment is left in place. )

Console.log (x); var x = 1;function x () {};

These three lines of simple code, var x = 1, will be split into Var x;x=1, two parts, declaration advance to the top of the current function, the assignment is left in place, and the function expression will be promoted to the top of the current function.

var x;function x () {};console.log (x); x=1;

Functions are executed in the order in which they were actually run, so the output X is function () {}.

  When there is no VAR declaration, you need to look for the variable declaration at the current function scope, and if not, go up one level, and note that if it is still not there, it will look up until the Window object, if there is no property in the Window object. The variable is then created in the window.

2, this point is determined by the way the function is called.

3. About constructors

The constructor cannot be called directly, and it must be automatically called when the object is created by the new operator, usually when the function is executed.

4. This represents the currently instantiated object in the constructor.

Topic

function Foo () {    getName = function () {alert (1);};} Foo.getname = function () {alert (2);}; Foo.prototype.getName = function () {alert (3);}; var getName = function () {alert (4);}; function GetName () {alert (5);};( 1) foo.getname ();(2) getName ();(3) Foo (). GetName ();(4) getName ();(5) New Foo.getname ();(6) New Foo (). GetName ();(7) New New Foo (). GetName ();

After understanding the above words, the first four questions should be very simple, after all, the variable declaration and declaration function advance to the top of the current function.

(1), direct call Foo.getname (), Output 2;

(2), getName (), function order execution, here we see the Var getName and function GetName functions expression is going to the Var getName to the top, the following function declaration in advance, then the first run functions GetName () { Alert (5);} ; after executing getname = function () {Aleert (4);}; The back is covered in front, output 4.

(3), obviously call the Foo function first, after the first call, here run GetName = function () {alert (1)} This is a function expression, we first see whether there is a variable declaration inside the current function, and not so found outside, var getName = function () {alert (4);}; This overwrites the function expression and makes a new assignment. Here return this, that is, execute this.getname (), which is called directly here, then this points to window. Output 1.

(4), after the previous step, GetName () is covered, still output 1.

5~7 questions

The main constructor is to instantiate through new.

(5), New (Foo.getname) (), GetName function is executed through the constructor, output 2

Here is an issue with the return value of a constructor function.

In the JS constructor can have a return value or not, we look like this

function f () {};>undefinednew f (); >f {}

If there is a return value, check to see if the value is a reference type , and if the non-reference type, such as the base type (string, number, Boolean, null, undefined) is the same as no return value, it actually returns its instantiated object.

If it is a reference type, the actual return value is the reference type.

function f () {return true;}; >undefinednew f (); >f {}
function f () {return {1: "A"}};>undefinednew f (); >object {1: "a"}

The return value here is This,this, which represents the currently instantiated object in the constructor, and therefore returns the current instantiation object. Because no attributes were added to the constructor in the Foo constructor, it was found in the prototype object (prototype) of the current object, looking for getname.

(6) (7) Output 3.

JavaScript Error Record variable definition elevation, this pointer pointing, operator precedence, prototype, inheritance, global variable pollution, object properties, and prototype attribute precedence

Related Article

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.