The essence of JavaScript

Source: Internet
Author: User

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.name!=null) {    a=car.name;  } else{a= ' unknown ';}     

Tips (for default values):

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

Usually

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

Tips:

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

Usually we will simply use the for in

var name;  In  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 jeep ={    run:function() {     ...    }}; 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) {Return a +b }var shuxue={}; Shuxue.calculate1 =function() {var add2 =function() {This.value=This.add (3, 3); } add2 ();//Function call pattern} shuxue.calculate1 ();//Method invocation pattern document.write (shuxue.value);//undefined shuxue.calculate2 = function () { var that=this; var add2 = function () {that.value=This.add (3, 3);} add2 ();  function call Pattern } shuxue.calculate2 (); // method call mode document.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, if the function is 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

        function add (A, b) {return a + b        ; } var result=add.apply (null,[3,4]); document.write (result); // 7 function GetName () {return this .name;} var car={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 scope ES6)

        var man=function(){var age=12return {Getyear: () {return age ; }, Setyear:function (n) {age= N;} }} var peter=man (); document.write (Peter.getyear ()); Span style= "color: #008000;" >//12 peter.setyear (20//20 document.write (peter.age); //undefined property private only accessible via interface         

Turn from (http://www.cnblogs.com/whosedream/p/5252089.html)

3. Cascade

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

    var man=function {var age=1            2return {Getyear: () {alert (age); return this;}, setyear : function (n) {age=N; Span style= "color: #0000ff;" >return this;}} var peter=man (); 
Peter.getyear (). Setyear (+). GetYear (); //

The essence of JavaScript

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.