The essence of Javascript [if I see it later or think of it, I'll continue to fill it up] [easy to understand].

Hello, I'm architecture Jun, an architect who can write code and recite poetry. Today I would like to talk about the essence of Javascript [if I see it later or think of it, I will continue to fill it up].

I'm just a little likable. How can I have any charming magic?

I grammar

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

II object

1. Usually assign the value of one object to another variable

if(car.name!=null){
    a=car.name;  
}else{
    a='unknown';
}

Tip (give default value):

a=car.name||"unknown";

 2. Trying to get a value from an object's properties

usually

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

Tips:

a=car&&car.age;

3. Enumeration properties

Usually we simply use for in

var name;
for(name in  car ){
   ...
}

The result is that the properties traversed out may be unpredictable and will traverse the properties on the prototype

Tip: instead of for in, use the for loop

var properties=[
     'name',
     'status',
      'useTime'
];

for(var i=0;i<properties.length;i++){
    car[properties[i]]  ...
}

4. Delete attribute

It is estimated that few people will use this thing, but its existence makes sense. I have encountered a scene. When using a third-party class library, the object created by the method provided by the class library has properties I don't need, and I want to convert this object into JSON format string. At this time, the function of deleting properties is particularly powerful.

Delete car.name ;

III function

1. Call

There are four calling methods in JS

  • Method invocation mode
  • Function call mode
  • Constructor call mode
  • apply call mode

The difference between the four modes is that this points to

The method call pattern is well understood. We write C# to define a class CAR and a method run for the CAR. At this time, an object jeep is instantiated, and the method call is jeep RUN();

The same is true in JS

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


jeep.run();

The point of this is the called object.

The function call mode is as follows: the add2 function is called, and the function name is written directly

        function add(a, b) {
            return a + b;
        }
       var shuxue={};
       shuxue.calculate1 = function () {
            var add2 = function () {
                this.value=this.add(3, 3);
            }

           add2();//Function call mode

        }
        shuxue.calculate1();//Method invocation mode
        document.write(shuxue.value);//undefined

        shuxue.calculate2 = function () {
            var that=this;
            var add2 = function () {
                that.value=this.add(3, 3);
            }

            add2();//Function call mode

        }
        shuxue.calculate2();//Method invocation mode
        document.write(shuxue.value);//6

If we call the function add2 to the function add2, we will find an exception in the code of the function add2. In this way, we will call the function add2 to the function add2, which will lead to an exception

value, calculate1 failed and calculate2 succeeded. The difference is that the latter gives this (this at this time points to the function) to the internal variable that of the function and fixes it. The embedded function uses the variables of the external function, that is, forms a closure. The former this points to the global.

Constructor call mode

var Car=function(){};

var jeep=new Car();  //There is a convention that if the function is used for new, it starts with uppercase to distinguish it from general functions

this points to the new object

apply call mode

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'};

        var name=getName.apply(car);//  car has no getName method
        document.write(name); //  jeep

this points to the first parameter of apply

2. Module

We know that JS can easily lead to the abuse of global variables, so JS provides a method to construct modules using closures and function scopes (JS has no block level scope before ES6)

        var man=function(){
            var age=12;
            return {
                getYear:function(){
                    return age;
                },
                setYear:function(n){
                    age=n;
                }
            }
        }

        var peter=man();
        document.write(peter.getYear()); //12
        peter.setYear(20);
        document.write(peter.getYear());//20
        document.write(peter.age); //The undefined property is private and can only be accessed through the interface

3. Cascade

This program has been loved by the majority of apes

    var man=function(){
            var age=12;
            return {
                getYear:function(){
                   alert(age);
                    return this;
                },
                setYear:function(n){
                    age=n;
                    return this;
                }
            }
        }

        var peter=man();
        peter.getYear().setYear(20).getYear();  //cascade

Added by dickey on Mon, 07 Feb 2022 02:49:21 +0200