JavaScript authoritative guide essence (i)

Source: Internet
Author: User

  1. How to bind events: IE8 and below,attachevent, other AddEventListener

  2. The functions in JavaScript are truth

  3. If the program uses a number where the string is expected to be used, the number is automatically converted to a string

  4. by x! =x can tell if a number is NaN

  5. The difference between substring and Slice is that they treat negative numbers differently, the former as 0, thelatter as the reciprocal

  6. since the string is not an object, why does it have attributes? Whenever a property of a string is referenced, the object inherits the method of the string and is used to handle the reference to the property. Once the property reference is finished, the newly created object is destroyed (not necessarily creating or destroying the temporary object on the implementation, but the whole process looks like this). Numbers and Booleans also have their own methods, and implementations are similar in principle,null and undefined do not wrap objects: Accessing their properties results in a type error.

  7. The conversion of an object to a string in JavaScript follows these steps

1) This method is called if the object has the ToString() method. If it returns a primitive value, Javascript Converts the value to a string (if it is not a string itself) and returns the string result.

2) If the object does not have the toString() method, or if the method does not return a raw value, then JavaScript calls the ValueOf() method. If this method is present, JavaScript calls it. If the return is the original value,JavaScript converts the value to a string (if it is not a string itself) and returns the result

3) Otherwise,JavaScript cannot get a raw value from toString() or valueOf(), so this is a type exception that it throws.

8. In the process of converting an object to a string,JavaScript does the same thing, except that it first tries to use the ValueOf() method:

1) If the object has the ValueOf() method, which returns a primitive value, Javascript converts the original value to a number (if necessary) and returns the number

2) Otherwise, if the object has the ToString() method, which returns a raw value, Javascript converts it and returns

3) Otherwise,Javascript throws an error exception

9. The details of converting objects to numbers explain why an empty array is converted to the number 0 and why an array with a single element is also converted to a number. The array inherits the default valueOf() method, which returns an object instead of an original value, so the array-to-number conversion calls the ToString() method. An empty array is converted to an empty string, and an empty string is converted to the number 0. The result of converting an array of elements into a string is the same as the result of converting the string to this element. If the array contains only a single numeric element, the number of the sanitation empty string, and then converted to a number.

The function scope of 10.Javascript is that all variables declared within a function are always visible inside the function body. Interestingly , This means that it is already available before the declaration. This feature of JavaScript is informally called declaration in advance, i.e. all variables declared in the JavaScript function (but not related to assignment) are "advanced" to the top of the function body.

11. When declaring a A Javascript global variable is actually a property that defines a global object (window). When declaring a variable with VAR, the property created is not configurable, meaning that the variable cannot be deleted by the delete operator. You may have noticed that Javascript automatically creates a global variable if you do not use strict mode and assign a value to an undeclared variable . A variable created in this way is a configurable property of the global object, and it can be deleted.

12. When a function is defined, it actually holds a scope chain. When this function is called, it creates a new object to store its local variables, adds the object to the saved scope, creates a new object to store its local variables, and adds the object to the saved scope chain, creating a new longer representation of the function call scope. Chain ".

13. When evaluating the value of an object creation expression, and the object initialization expression is passed {} creates an object the same way. JavaScript First creates a new empty object, and thenJavaScript invokes a specified function by passing in the specified parameter and using the new object as the value of this. This function uses this to initialize the properties of this newly created object. Those functions that are considered constructors do not return a value, and the newly created and initialized object is the value of the entire object creation expression. If a constructor does return an object value, the object creates the value of the expression as an entire object, and the newly created object is abolished.

Lvalue: An expression can only appear to the left of the assignment operator.

14.eval() has the ability to change global variables, and when called by an alias,eval() executes its string as the top-level global code. Code that executes may define new global variables and global functions, or global variables, but cannot use or modify local variables in the keynote function, so this does not affect code optimization within the function. The instance code is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4C/8B/wKiom1Q_jIuBDvNHAAFPhnxcRhA602.jpg "title=" 1.png " alt= "Wkiom1q_jiubdvnhaafphnxcrha602.jpg"/>

IE9 Earlier versions of IE were different from other browsers, and when eval() was called through an alias, it was not a global eval () (it would also throw a evalerror exception, Call it only as partial eval ). But IE does define a global function called Evalscript () to complete the function of global eval (but slightly different from eval (),ExecScript () will always return null.

15.Javascript:void(0), specifying to evaluate an expression but not return a value,a href=# with a HREF=JAVASC the difference between ript:void (0)

# contains a location information

The default anchor is #top the top of the page

and =javascript:void (0) only represents a dead link

16. Function declaration statements typically appear in the The top-most layer of Javascript code can also be nested within other function bodies, but when nested, function declarations can only be present at the top of the nested function. That is, the function definition cannot appear in the IF statement,while loop, or any other statement, precisely because of this limitation of the position of the function declaration,the ECMAScript Standard specification does not classify the function declaration as a true statement. Some Javascript implementations do allow function declarations where statements occur, but different implementations differ greatly in how they are handled, so the practice of placing function declarations within other statements is not portable.

Although function declaration statements and function definition expressions contain the same function names, they are still different. Both methods create a new function object, but a function declares a function in a statement, a variable name, and a variable that points to a function object. as with Var , a function in a function definition statement is explicitly "advanced" to the top of a script or function. Therefore, they are visible throughout the script and in functions. With var , only the variable declaration is ahead of time-the initialization code of the variable is still in its original position. With function declaration statements, however, both function names and function bodies are in advance: all functions in the script and all nested functions in functions are declared before other code in the current context. That is, you can declare a Javascript function before calling it again.

As with the Var statement, the variables created by the function declaration statement cannot be deleted. However, these variables are not read-only and variable values can be overridden.

The match operation for each case in 17.switch actually compares the city "= = ="identity operation rather than "= =", so the expression and case The match does not make any type of conversion


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1564875

JavaScript authoritative guide essence (i)

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.