Summary of the JavaScript language essence knowledge points

Source: Internet
Author: User
Tags hasownproperty

1.NaN is a numeric value that represents a result of an operation that does not produce a normal result. nan is not equal to any value, including its own.

2.Infinity represents all values greater than 1.79769313486231570e+308, so infinity is actually not a specific value, it is a collection.

All characters in 3.JavaScript are 16-bit

4. The values listed below are treated as false:

    • False
    • Null
    • Undefined
    • Number 0
    • Value Nan
    • Empty string ""

5. All other values are treated as true, including true, with the character "false"

The 6.for loop has the form:

The first is a common form: for (initialization clause; conditional clause; increment clause)

The second: The For in statement enumerates all the property names (or key names) of an object. In each loop, the next property name string for object is assigned to variable.

Typically used to detect object.hasownproperty (variable) to determine whether the property name is a member of the object or from a prototype chain.

Use the following:

1  for inch obj) {2      if (Obj.hasownproperty (MyVar)) {//) returns True if the object is unique to the property. The method does not return the prototype chain 3           ... 4      }5 }

7. Operator Precedence: top-to-bottom:

    • . [] (): Extract property and Call function
    • Delete new typeof +-!: unary operator
    • */%: multiplication, division, take-up (in fact, the residual operation, especially shown in two operands in the presence of negative numbers, the value of the modulo operation and the remainder operation is not the same)
    • +-: addition/connection, subtraction
    • >= <= > <: Inequality operators
    • = = =!==: inequality operator
    • &&: Logic and
    • || : Logical OR
    • ?: Ternary

8. Numbers, strings, and Booleans are "seemingly" objects, but are immutable. An array, function, regular expression is an object.

9. Literal:

var empty_object={};

Or

var stooge={

"First-name": "Jerome",

"Last-name": "Howard"

};

Or

var flight = {
Airline: "Oceanic",
number:815,
Departure: {
IATA: "SYD",
Time: "2004-09-22 14:55",
City: "Sydney"
},
Arrival: {
IATA: "LAX",
Time: "2004-09-23 10:42",
City: "Los Angeles"
}
};

The property name can be any string, including an empty string. If the property name is a valid JavaScript identifier and is not a reserved word, it is not mandatory to enclose the property name in quotation marks.

Attempting to retrieve a value that does not exist for a member property returns undefined, and attempting to take a value from the undefined member property results in a TypeError exception, which can be avoided by the && operator.

For example:

Flight.equipment && flight.equipment.model//undefined, the first is a non-existent attribute, the second is the value of a property that does not exist

10. Prototypes and Inheritance:

First define a variable:

1 var stooge = {2     "First-name": "Jerome",3     "last-name": "Howard" 4 };

When you need to create a new object, you can select an object as his prototype. This adds a create method to object, which creates a Stooge object to use as its prototype.

1 if(typeofobject.beget!== ' function '){2Object.create=function(o) {3               varf=function(){};4F.prototype=o;5               return Newf ();6        };7 }8 varAnother_stooge=object.create (stooge);

When we make a change to an object, it does not involve the object's prototype:

1 another_stooge[' first-name '] = ' Harry '; 2 another_stooge[' middle-name '] = ' Moses '; 3 another_stooge.nickname = ' Moe ';

The prototype link is only used when retrieving values. If we try to get a property value for an object, but the object does not have this property name, JS attempts to get the property value from the prototype. If there is no attribute value in the prototype object, it will be found in the prototype of the prototype object, and so on, until the Object.propotype is reached. If the property you want to find does not exist with the prototype chain, the result is the undefined value. This process is called a delegate .

The For In loop iterates through all the property names in an object. The enumeration procedure lists all properties-including those in functions and prototypes. If you want the values of the properties in the object, you can use Hasownprototype and typeof, for example:

1 var name; 2  for inch Another_stooge) {3     if(typeof another_stooge[name]!== "function") {4         Document.writein (name+ ":" +another_stooge[name]); 5     }6}7               

The order in which the property names of the above methods appear is indeterminate.

The

Delete: delete operator can be used to delete an object's properties. If the object contains this property, the property is removed and does not touch any objects in the prototype chain . Deleting a property in an object may cause properties from the prototype chain to appear, for example:

1 another_stooge.nickname    //  ' Moe '23//  Remove Nickname from Another_stooge, revealing4// the nickname of the prototype. 5 6 Delete Another_stooge.nickname; 7 8 another_stooge.nickname    //  ' Curly '

11. Reduce Global Pollution:

The first method: Create a unique global variable, and the rest of the global variables are its properties.

1 varMYAPP = {};2 3Myapp.stooge = {4"First-name": "Joe",5"Last-name": "Howard"6 };7 8Myapp.flight = {9Airline: "Oceanic",Tennumber:815, One Departure: { AIATA: "SYD", -Time: "2004-09-22 14:55", -City: "Sydney" the     }, - Arrival: { -IATA: "LAX", -Time: "2004-09-23 10:42", +City: "Los Angeles" -     } +};

The second method: closures

12. When the number of actual arguments of a function exceeds the number of formal arguments, the arguments that are exceeded are ignored. If the actual parameter is too small, the missing value is replaced with undefined.

13.this is very important in object-oriented programming, and his value depends on the mode of the call. There are 4 invocation modes in javascript:

The first: Method invocation pattern: When a function is saved as a property of an object, we call it a method. When a method is called,this is bound to the object , and if the invocation expression contains an action that extracts the property (including an. expression or subscript expression), then he is called as a method.

For example:

1 varMyObject = {2value:0;3Incrementfunction(inc) {4          This. Value + =typeofinc = = = ' Number '? Inc:1;5     }6 };7 8 myobject.increment ();9Document.writeln (Myobject.value);//1Ten  OneMyobject.increment (2); ADocument.writeln (Myobject.value);//3

Second, the function invocation pattern: When an object is not a property of an object, it is called as a function. When a function is called in this mode, thisis bound to the global object . This way, you cannot implement the method's access rights to the object. There is a workaround:

If the method defines a variable and copies it to this, the intrinsic function can access the this through the newly assigned variable.

Let's call this new assignment variable.

For example:

1<script type= "Text/javascript" >2     varValue=4;3           varMyObject = {4value:0,5 };6 7 8MyObject.Double=function (  ) {9     //var = this; Workaround.TenConsole.log (this.value);//0 One     varHelper =function (  ) { AConsole.log ( This. value);//4 -     }; -  theHelper ();//invoke helper as a function - }; -  - //call a double in the form of a method +  -MyObject.Double(  ); +Document.writeln (Myobject.value);//0 A</script>

In the preceding code, double is called in the form of a method, so if the double contains this, the this point points to the MyObject, so the 10th line outputs the value of MyObject internal, and values 0. Inside a double method, there is a helper function called as a function, so the global variable in the helper function is the value of the global variable, which is 4.

In this form, helper is unable to access other properties inside the MyObject object through this. If Var is that=this, add it to the that variable inside the double function, and then change the code inside the helper function to Console.log (that.value); This can point to value inside the MyObject, and the output is 0. The code is as follows:

1<script type= "Text/javascript" >2     varValue=4;3           varMyObject = {4value:0,5 };6 7 8MyObject.Double=function (  ) {9     varthat = This;//workaround.TenConsole.log ( This. Value);//0 One     varHelper =function (  ) { A Console.log (that.value);//0 -     }; -  theHelper ();//Invoke Helper as a function. - }; -  - //Invoke double as a method. +  -MyObject.Double(  ); +Document.writeln (Myobject.value);//0 A</script>

The third pattern, the constructor invocation pattern, is invoked with new before the function, then secretly creates a new object connected to the prototype member of the function, which is bound to the new object. For example:

1 varquo=function(string) {2            This. value= "String";3 };4 5 6quo.prototype.get_status=function(){7           return  This. Value;8 };9 Ten varmyquo=NewQuo ("confused"); OneConsole.log (Myquo.get_status ());//confused

Fourth invocation pattern: Apply Call:

The Apply method lets us construct a parameter array to pass to the calling function. It run Xu we choose the value of this. Apply receives two parameters, the first is the value to bind to this, by setting this parameter can change the function's runtime context, that is the value of this, if it is set to null or does not specify the parameter, then this specifies itself.

Summary of the JavaScript language essence knowledge points

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.