The essence of JavaScript "If I see it later or think about it, go ahead and fill it up."

Source: Internet
Author: User

I just have a few pleasing places, how can there be any fascinating magic?

I. Grammar

JS has only one numeric type, 64-bit floating-point number, so 1 and 1.0 are the same. Why this design: Prevent short-integer overflow.

Two. Object 1. An assignment method that typically assigns the value of an object to another variable
if (car.status!=null) {    a=car.status;  } Else {    A= ' unknown ';}

Tips (for default values):

a=car.status| | " Unknown ";
2. Try to take a value from a property of an object

Usually

if (car!=undefined) {   a=car.status;}        

Tips:

a=car&&car.status;
3. Enumeration properties

Usually we will simply use the for in

var name;  for inch   car) {   ...}

The consequence of this is that the traversed properties may not be predictable and will traverse the properties on the prototype.

Tip: Do not apply for in, instead use for loop

var properties=[     ' name ',     ' status ',      ' usetime '];  for (var i=0;i<properties.length;i++) {    car[properties[i]]  ...}
4. Delete Attributes

This thing is estimated that very few people will use, but there are some reasons, I have encountered a scene, in the use of a third-party class library, the class library provides methods to create objects I do not need properties, and I want to convert this object into a JSON-formatted string, this time to delete the properties of this feature is particularly powerful.

Delete Car.name;
Three. Function 1. Call

There are 4 kinds of calling methods in JS

    • Method invocation Pattern
    • Function call pattern
    • constructor invocation Pattern
    • Apply Call pattern

The difference between the four modes is that this point

Method call Pattern well understood, we write C # define a class car, give CAR define a method RUN, this time instantiate an object Jeep , then method call is Jeep. RUN ();

JS in the same way

var car ={    run:function() {     ...    }}; var jeep =new  car (); Jeep.run ();

This points to the object that is called.

function call pattern, the following add2 function is called, the direct write functions the name of

        functionAdd (A, b) {returnA +b; }       varShuxue={}; Shuxue.calculate1=function () {            varADD2 =function () {                 This. value= This. Add (3, 3); } add2 ();//function Call Pattern} shuxue.calculate1 ();//Method Invocation Patterndocument.write (Shuxue.value);//undefinedShuxue.calculate2=function () {            varthat= This; varADD2 =function() {That.value= This. Add (3, 3); } add2 ();//function Call Pattern} shuxue.calculate2 ();//Method Invocation Patterndocument.write (Shuxue.value);//6

This point here is a bit of a problem, JS design point to the global, if this will lead to the exception of the above code, the code defines a Shuxue object, and gave him a definition of 2 methods calculate1 and Calculate2, We found that the ADD2 function was also called inside the function, and the ADD2 function attempted to add an attribute to the object.

The way of Value,calculate1 failed, the Calculate2 way succeeded, the difference is that the latter put this (at this time this is pointing to the function) gave the function an internal variable that, fixed it down, The inline function uses the variables of the external function, that is, to form a closure, which this points to the global.

constructor invocation Pattern

var car=function() {}; var jeep=New Car ();  // There is an agreement that if the function is to be used for new, then capitalize the beginning to distinguish it from the general function

This points to this new object

Apply Call pattern

JS is a functional object-oriented programming language, so functions can have methods

        functionAdd (A, b) {returnA +b; }        varResult=add.apply (NULL, [3,4]); document.write (result);//7        functionGetName () {return  This. Name; }        varCar={name: ' Jeep '}; varName=getname.apply (car);//car doesn't have a getname method .document.write (name);//Jeep

This points to the first parameter of apply

2. Module

We know that JS is prone to misuse of global variables, so JS provides a way to construct modules using closures and function scopes (JS without block-level scopes)

        varman=function(){            varAge=12; return{getYear:function(){                    returnAge ; }, Setyear:function(n) { age=N; }            }        }        varPeter=Man (); document.write (Peter.getyear ()); // APeter.setyear (20); document.write (Peter.getyear ());// -document.write (Peter.value);//undefined property Private can only be accessed through the interface

3. Cascade

This is already a favorite of the vast number of procedural apes.

    varman=function(){            varAge=12; return{getYear:function() {alert (age); return  This; }, Setyear:function(n) { age=N; return  This; }            }        }        varPeter=Man (); Peter.getyear (). Setyear (). GetYear ();//Cascade

The essence of JavaScript "If I see it later or think about it, go ahead and fill it up."

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.