Javascript Crusoe (on) __ objects, packaging

Disclaimer: This article is a blogger original, reproduced, please attach a blog link. https://blog.csdn.net/AquamanTrident/article/details/90576069

First, the object
    1, create an object, the object has some properties and methods.

            var obj = {
                name : 'object',
                age : 20,
                gender : 'bisexual',
                difficult : function(){
                    console.log('你能把我咋地!');
                    this.age--;
                },
                length: function(){
                    console.log('大丈夫能屈能伸');
                    this.age++;
                }
            }

           By: obj.father = 'Javascript';
           Delete: delete obj.name;
           change: obj.gender = 'female';
           search: console.log (obj.age);
           if not access an object property, returns undefined.
    2, an object creation method:
          . 1, plainObject, literal objects / object directly amount, with reference to the above code.
          2, the constructor (named Big camel)
                1. System comes: var obj = new new Object ();
                2. custom methods, see figure codes.

            function CarManufacture(color,seat){
                this.color = color;
                this.seat = seat;
                this.name = 'BMW';
                this.height = '1400';
                this.weight = 1000;
                this.health = 100;
                this.run = function(){
                    this.health--;
                }
            }
            var car1 = new CarManufacture('red','leather');
            var car2 = new CarManufacture('yellow','wood');

                A factory to produce two identical cars, car1 and car2, they can call free to own properties and methods, independent of each other.
         3, the internal structure of the principle functions:
              1. At the top of the implicit function body var this = {};
              performed XXX = 2. this.xxx;
              3. Finally, the implicit return this;

            function Person(name,height){
                //var this = {}
                this.name = name;
                this.height = height;
                this.say = function(){
                    console.log(this.say);
                }
                //return this;
                return {};//return引用值可以捣乱,但return原始值影响不了。
            }

            var person1 = new Person('Jack',180);
            var person2 = new Person('Tom',160);


Second, the packaging categories:
       properties and methods exclusive objects, the original value is not determined properties and methods! ! !
       Numbers, strings, Boolean are new. However, this operation is not null and undefined, which is not the reason They call it toString
       var NUM = Number The new new (123);
       var STR = new new String ( 'ABCD');
       var = BOL new new Boolean ( 'to true');  

            var str = 'abc';
            str += 1;
            var test = typeof(str);//test == 'string'
                //隐式new String(test).length == 6    delete
            if(test.length == 6){
                test.sign = 'typeof的返回结果可能为String';
                //隐式new String(test).sign = 'typeof的返回结果可能为String'   delete!!!
            }
                //隐式new String(test).sign = undefined
            console.log(test.sign);//undefined

The above content is a brother original, finishing from " crossing a Javascript education curriculum ", a recommended " crossing an education ."

 

Guess you like

Origin blog.csdn.net/AquamanTrident/article/details/90576069