js objects and functions complement

JS Object complement (to master)

    // 1) js not a dictionary, only the type of the object, the object may completely replace the use of a dictionary 
    var dic_obj = { 
        // attribute: value (value function) 
        'name': 'Bob', 
        'EAT': function () { 
            console.log ( 'eating') 
        } 
    }; 

    the console.log (dic_obj.name, dic_obj [ 'name']); 
    dic_obj.eat (); dic_obj [ 'eAT'] (); 

    // 2) js objects the property name, string type are used, it can be omitted quotation marks string 
    var obj = { 
        name: 'Tom', 
        eAT: function () { 
            the console.log ( 'eating ...') 
        } 
    }; 

    the console.log (obj.name, obj [ 'name']); 
    obj.eat (); obj [ 'EAT'] ();

    // 3) when the object attribute value functions, called methods, method recommended shorthand method name () {} 
    var obj2 = { 
        name: 'Tom',
        EAT () {
            console.log ( 'eating ...') 
        } 
    }; 

    the console.log (obj2.name, obj2 [ 'name']); 
    obj2.eat (); obj2 [ 'EAT'] (); 

    //. 4) If the attribute value of an object is a variable, and the variable names and attribute names are the same, properties may also be abbreviated {,} 
    var height = 180 [; 
    var {P = 
        height, 
        name: 'Jerry' 
    }; 
    the console.log (p.name, p.height);

Method declaration class (understand)

    JS classes // 
    // first method of a class declaration 
    class People { 
        constructor (name) { 
            this.name name = 
        } 
        EAT () { 
            the console.log (this.name + 'eating') 
        } 
    } 
    the let P1 People new new = ( 'Bob'); 
    the let new new People P2 = ( 'Tom'); 
    the console.log (p1.name, p2.name); 
    p1.eat (); 

    method // second class declaration (difficulties ): this syntax within a function, the function-based presentation, or is a normal function 
    function Teacher (name) { 
        this.name = name; 
        this.eat = function () { 
            the console.log (this.name + 'eating' ) 
        } 
    } 

    the let new new Teacher T1 = ( 'Owen'); 
    t1.eat ();

Class attribute (master)

    // class properties: property class assigned to all objects can access 
    function the Fn () {} 
    the let new new the Fn = F1 (); 
    the let F2 = the Fn new new (); 
    // attribute class assignment 
    Fn.prototype.num = 100; 

    the console.log (f1.num); 
    the console.log (f2.num); 

    // singleton similar 
    Vue.prototype.num = 1000; 
    the let new new Vue V1 = (); 
    the let new new Vue V2 = (); 
    Console. log (v1.num); 
    the console.log (v2.num);

JS function complements

    // 1) shape functions involved when an incoming call argument relationship (you pass you, I received my) 
    // incoming and does not require the same number of parameters accepted, but must be assigned by location (no key word parameter) 
    // not accepted abandoned real participants, participants are not assigned is assigned shaped undefined 
    function Fn1 (a, B) { 
        return a + B; 
    } 

    the let Fn1 RES = (10,20); 
    Console .log (res); 

    Evolution // 2) function defined in 
    the let Fn2 = function (a, B) { 
        return a + B; 
    }; 

    // function omitted arrow keys 
    let fn3 = (a, b) => { 
        a + B return 
    }; 

    // no function body, only the return value of the function may be omitted scope {}, since only the return value, return is omitted so 
    the let Fn4 = (a, B) => a + B; 
    Console. log (Fn4 (11,22)); 

    // if only one parameter, the parameter declaration () may be omitted  
    let fn5 = num => num * num;
    the console.log (Fn5 (. 3)); 

    // language type JS is weak
    console.log(10 + '5');  // 105
    console.log(10 - '5');  // 5
    console.log(+'55555');  // 数字55555

 

Guess you like

Origin www.cnblogs.com/baohanblog/p/12297261.html