Excerpt from the Book of JavaScript revelation

Source: Internet
Author: User
Tags list of attributes hasownproperty

1. JavaScript pre-packaged 9 native object constructors:

Number (), String (), Boolean (), Object (), Array (), Function (), Data (), RegExp (), Error ()

2, the above object returns a type other than function returns function, the other returns an object.

varmystring=NewString (' foo '); Console.log (typeofmyString); varmyregexp=NewREGEXP (' \\bt[a-z]+\\b '); Console.log (typeofmyregexp); varmyfunction=NewFunction ("x", "Y", "return X+y")); Console.log (typeofmyFunction); varMynumber=NewNumber (23); Console.log (typeofMyNumber); varmyobject=NewObject (); Console.log (typeofmyObject); varmyarray=NewArray (' foo ', ' Bar '); Console.log (typeofMyArray); varMydata=NewDate (); Console.log (typeofmyData); varMyerror=NewError (' crap! '); Console.log (typeofmyerror); varmyboolean=NewBoolean (false); Console.log (typeofMyboolean);

3, native object, custom object constructor code comparison

varperson=funtion (living,age,gender) { This. living=living;  This. age=Age ;  This. gender=gender;  This. Getgender () {return  This. Gender;} }            varCodya=NewPerson (true, "male");//Custom Constructors            varcodyb=NewObject; Codyb.living=true;//Native Object

4, complex value: the Native object Constructor object (), Array (), function (), Data (), RegExp (), Error () is a complex type because they contain one or more original values or complex values. P18

5, the original value (simple value) copy is the value of the copy, the complex value is copied at the time of the copy of the address, that is, copied in the way referenced.

6. When you create a string (), Number (), Boolean () value using the New keyword, or if the values are converted to complex objects in the background, they do not have the characteristics of complex calculations by reference, even though the original values can be considered complex values.     Distinguish it from the 5th. P20

7, complex object comparison is according to the address comparison, that is, if the two comparison of the object points to the same address, then output true, otherwise output false, compared to simple values by value comparison. It is also important to note that the comparison symbol for complex objects is = = =, note is three =.

8, delete is the only way to delete an object property, because setting the object's property to undefined or null only changes the value of the property, not the property from the object. Note that Delete does not delete the properties found on the prototype chain.

var foo={bar: ' Shabi '};             Delete Foo.bar;            Console.log ( in foo);

The result is output false, because the bar property of the Foo object has been deleted.

9. The hasOwnProperty function can detect whether a property is from the prototype chain, returns True when it comes from the object itself, and returns False when it comes from the prototype chain.

var mytest={foo: ' Value '};            Console.log (Mytest.hasownproperty (' foo '));    // returns True            Console.log (Mytest.hasownproperty (' toString '));    // returns false

10, different from the 9th, when we use the in operator to find a property, he will not only find his own list of attributes, but also through the prototype chain lookup, and once found this property, will return true. P37

var mytest={foo: ' Value '};            Console.log (Mytest.hasownproperty (' foo '));    // returns True            Console.log (Mytest.hasownproperty (' toString '));    // returns false             in myTest);            // returns True

11. The for-in loop not only iterates through the local properties of a particular object, but also iterates over the attributes on the object that is inherited (through the prototype chain), so we need to include the hasOwnProperty () function within the loop to filter out which properties are found through the prototype chain.

var cody={age:23,gender: ' Male '};              for (var in Cody)            {                if(Cody.hasownproperty (key))                {console.log (key);}         // output age and gender            }

12. Both JavaScript and C + + variables are named after letters, underscores, and strings of letters, numbers, and underscores.

13, the function will return the value, when we do not explicitly set the return value, the function returns undefined. P52

14. Self-calling function: Call the function expression immediately after defining the function expression. P59

var sayword=function() {Console.log (' Handsome B ')} ();

15. In JavaScript, there are only global scopes, local scopes (function scopes), Evel scopes.  Unlike C + +, it does not have a block scope, that is, scopes cannot be created within the IF, for statement blocks, and variables can be overwritten with each other. P78

vartemp=10;//Temp is valid everywhere including the test function and the eval statement            vartest=function() {console.log (temp); vartemp1=100;//Temp1 only valid within the scope of the test function            }(); Eval (' var temp2=1000;console.log (temp); ');//Temp2 only valid in eval statementsConsole.log (temp);    Console.log (TEMP1); //output undefinedConsole.log (TEMP2);//output undefined

16. JavaScript declares a variable that is missing Var as a global variable, even if the variable is in the body of the function. P78

var temp=10;            // Temp is valid everywhere including the  test function and the eval statement            var test=function()            {    Console.log (temp);                Temp1=100;        // Temp1 Front no var  at this time he is the global scope of             } ();            Eval (//Temp2 front without var  at this time he is the global scope of             console.log (temp);            Console.log (TEMP1);     // Output            // Output

17. Closure Package:

 var  count=function   () { var  i=0;  return  function   () { return  i++; }} ()  //  here () do not forget to call the Count function immediately from the    The function of the  Console.log (count ()); //     output 0  Console.log (count ()); //     output 1  console.log (count ()); //  output 2  

18, there is no new keyword is important:

varMyboolean=boolean (0);//There is no use of the new keyword as raw value processing            if(Myboolean) Console.log (' Myboolean is true '); ElseConsole.log (' Myboolean is False '); varmyboolean1=NewBoolean (0);//use the New keyword False Boolean object is actually a true object            if(myBoolean1) Console.log (' MyBoolean1 is true '); ElseConsole.log (' MyBoolean1 is false ');

Output Result:

You can see that the output is false when the New keyword is not used;

The output is true when the new keyword is used. (Non-Raw false Boolean object converted to True) P111

19. For a variable that has a null value, the typeof operator returns an object. P117

var myobject=null;            Console.log (typeof  myObject);            Console.log (myObject= = =null);

The output is:

Excerpt from the Book of JavaScript revelation

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.