JavaScript对象

对象构建方法

1.对象字面量/对象直面量

var obj = {};

2.构造函数

    // 1.系统自带的构造函数Object()
    var obj = new Object();

    // 2.自定义(自定义构造函数名第一个字母建议大写)
    function Car(color){
        this.color = color; 
        this.name = "BMW";
        this.height = "1000";
        this.lenght = "5500";
        this.health = 100;
        this.run = function (){
            this.health --;
        }
        //...
    }
    var car1 = new Car('red');
    var car2 = new Car('green');

构造函数内部原理
// 当系统使用new关键字调用该函数时,系统会在函数前面隐式创建一个空对象,然后再函数末尾返回this;

    function Car(color){
        // var this = {}
        this.color = color; 
        this.name = "BMW";
        this.height = "1000";
        this.lenght = "5500";
        this.health = 100;
        this.run = function (){
            this.health --;
        }
        //...

        // return this;
    }

对象操作方法:增、删、改、查

 

原文地址:https://www.cnblogs.com/wood2012/p/7896547.html