Objects and wrapper classes

1. Object creation method

 // 1.var MrDeng = {} plainObject object literal quantity / object direct quantity
            var MrDeng = {
                // attribute
                name : "MrDeng",
                age    : 40,
                sex : "male",
                health : 100,
                // Method
                smoke : function(){
                    console.log('I am smoking ! cool!');
                    //Note that unlike C++ classes, attributes need variable names, otherwise they are not recognized. Or use this - > this. health -.
                    MrDeng.health--;
                },
                drink : function(){
                    console.log('I am drinking');
                    MrDeng.health++;
                }


            }


2. Constructor
          

 // 1) The constructor Object() of the system
            var obj = new Object();  // var obj = {};
            // 2) There is no difference between the constructor and the function in structure, but it must obey the big hump. A common function is a hump.
            function Car(){
                this.name = "BMW";

            }

            var car1 = new Car();
            //Custom property values
            function Person(name){
                this.name = name;

            }
            var man =  new Person('laowang');

        //If you need to add attributes, you can add them directly here without adding them to the above code.
        MrDeng.wife = 'xiaoli';
        //Adding method
        MrDeng.eat = function(){
            console.log('I am eating');
        }
        //Deleting attributes can also be done this way
        delete MrDeng.sex;
        delete MrDeng.drink;
        // Be careful:
        //An error is reported when a variable is used without a declaration. When an object's properties are used without declaration, undefined is prompted.

3. Internal Principles of Constructors
// 1) Implicitly add var this = {} at the front of the function body
// 2) Execute this.xxx = xxx;
// 3) Implicitly returning this can show return {} to interfere with returning this

4. packaging category
// The original value cannot have attributes and methods. number
var num = new Number(123); // This is the object, not the original value
Var a = 123123'; // This is the original value, and the reason why he has the method is that he has passed through the wrapper class.
console.log(a.length);//1. Return the length of the new object through the wrapper class new String('123123') 2. 3 delete the new object.
a.length = 2; // This only modifies the length of the new object and does not change the length of the string a.
 

Tags: Attribute

Posted on Sat, 12 Oct 2019 08:24:24 -0700 by mosizlak