JS中的对象

//创建一个对象
var person = {

    name : 'Michael',
    age : 35,
    books : ['大话设计模式','大话数据结构','Swift从入门到精通'],
    say : function (words) {
        console.log(this.name + '说:' + words);
    },

    run : function () {
        console.log(this.name + "在走路");
    }
};

//使用对象,给对象的属性赋值,调用对象的函数
person.age = 32;

person.name = '大傻逼';

console.log(person);

person.run();

person.say('雾草');

使用构造函数批量创建对象(和面向对象语言的类相似)

//创建一个构造函数
var Car = function () {
    this.color = '红色';
    this.name = '奔驰';

    this.run = function(where) {
        console.log(this.name + '正在' + where + '奔跑')
    }
};

//创建第一个Car对象
var car1 = new Car();
console.log(car1.color,car1.name);
car1.run("操场");

//创建第二个Car对象
var car2 = new Car();
car2.name = '兰博基尼';
car2.color = '白色';
console.log(car2.color,car2.name);
car2.run('马桶')

results matching ""

    No results matching ""