Getting the js objects

First, understand the object

In the real world things are objects, everything is an object object or a specific thing 

objects: features and behavioral characteristics that make up the term used to describe the object, the behavior of the verb 

objects in the program 
is the real world of things abstract
 1 JS. in fact, an abstract object life
 2 . JS objects is a set of unordered attributes
 3 . property which may contain a base value, or a function of an object, the object is not a set value of the order of
 4 behavior object and characteristic 
    behavior: method 
    characteristics: the properties 

how to create an object, the object's members how to use 
objects: properties and methods are called members 

this object is represented in the current object

1. literal create objects

var Dog = { 
        name: 'Puppy' , 
        type: 'Chinese rural dogs' , 
        Age: 2 , 
        Color: 'Yellow' , 
        Bark: function () { 
            the console.log ( the this .name + 'Wang Wang Wang' ) 
        }, 
        EAT: function () { 
            the console.log ( the this .name + 'Gutou' ) 
        } 
    } 

property access 
the console.log (dog.name); 
the console.log (dog.type); 

another method of access attribute 
console. log (Dog [ 'name']); 

Method of accessing 
dog.bark (); 
dog.eat (); 

functions and methods of difference 
functions: independent existence is a function of the function name () call 
. Method: function object belonging to the object method name () call

 

2. new Object to create objects and dynamically add attributes

2. new Object () 
Object constructor is a 
new way to invoke the constructor 
new Object () calls the constructor creates an object in memory 

// dynamically add properties and methods 
    var Hero = new Object (); to create a null object 
    hero.name = ', CHINA' ; 
    hero.weapon = '' attack; 
    hero.equipment = [ 'TK', 'GJ', 'kJ' ]; 
    hero.attack = function () { 
        the console.log ( the this . + name ': Attack lalala' ) 
    } 
    hero.attack ();

3. factory function to create an object

function createHero (name,weapon,equipment,blood) {
        var hero = new Object(); 
        // 返回一个空的对象
        hero.name = name;
        hero.weapon = weapon;
        hero.equipment = equipment;
        hero.blood = blood;
        hero.attack = function () {
            console.log(this.name + ': 攻击')
        }
        return hero;
    }

    var hero1 = createHero('huangzhong','gongjian',['toukui','xuezi'],100);

 

4. Custom constructor creates objects

Name: Pascal comply with the rules, the first sisters first word capitalized, the follow-up of each word capitalized
 function Hero (name, Weapon, Equipment's, Blood) {
         // this dynamic objects to increase the membership of this points to the current object 
        this = .name name;
         the this .weapon = Weapon;
         the this .equipment = Equipment;
         the this . Blood = Blood;
         the this .attack = function () { 
            the console.log ( the this .name + 'Attacking' ) 
        } 
    } 

    var hero1 = new new Hero ( 'huangzhong', 'gongjian' , 'toukui', 100)

 

Two, new keywords, and this

Execution 1. new keywords

1 . Create an empty memory in the object
 2 . Let this constructor pointing object just created
 3 . Constructor is executed, and a method of setting functions in the constructor
 4. Return the current object

2. this appears three places

1. In the process in   this point is the object of this method belongs to
 2 . Window functions pointed
 objects 3. In this construction method is the constructor Create

 

Third, the operation of the object case

Case 1: for in traverse the object

var obj = {
        name: 'zs',
        age: 18,
        sex: 'male',
        sayHi: function () {
            console.log(this.name + ': hello');
        }
    }
    for (var key in obj) {
        console.log(key + '---' + obj[key]);
    }
View Code

Add property

for (var i = 0; i < 10; i++) {
        o['a' + i] = i * 2
    }
View Code

Remove members delete

var o = {
        name: 'abc',
        say: function () {
        }
    }
console.log(o.name);
delete o.name;
console.log(o.name);
View Code

 

Guess you like

Origin www.cnblogs.com/guniang/p/11987255.html