多种方式给JS对象添加属性和方法

方式一:定义对象时,直接添加属性和方法

function Person(name,age,sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.code = function(){
        console.log(this.name + " is coding");
    }
}

var xiaoming = new Person("xiaoming",10,"man");
console.log(xiaoming);// {name: "xiaoming", age: 10, sex: "man", code: ƒ}
xiaoming.code();// xiaoming is coding 

运行结果:

Person {

  name: 'xiaoming',

  age: 10,

  sex: 'man',

  code: [Function (anonymous)]

}

Xiaoming is coding

 
方式二:通过"对象.属性名"添加属性和方法

function Fruit(){}

var tomato = new Fruit();
tomato.name = "xihongshi";
tomato.color = "red";
tomato.use = function(){
    console.log(this.name + " can be to eat");
}
console.log(tomato);
tomato.use();

运行结果:

Fruit { name: 'xihongshi', color: 'red', use: [Function (anonymous)] }

Xihongshi can be to eat

方式三:通过"对象['属性名']"添加属性和方法

function Fruit(){}

var tomato = new Fruit();
tomato['name'] = "xihongshi";
tomato['color'] = "red";
tomato['use'] = function(){
    console.log(this.name + " can be to eat");
}
console.log(tomato);
tomato.use();

运行结果:

Fruit { name: 'xihongshi', color: 'red', use: [Function (anonymous)] }

Xihongshi can be to eat

方式四:通过 prototype (原型)添加属性和方法

function Animal(){};

Animal.prototype.foots = 4;
Animal.prototype.weight = 200;
Animal.prototype.hobby = "sing";
Animal.prototype.have = function(){
    console.log("the animal have " + this.foots + " foot");
}

var pig = new Animal();
console.log(pig);
pig.have();// the animal have 4 foot

运行结果:

Animal {}

the animal have 4 foot

方式五:使用Object.assign添加属性和方法

function Person(name,age,sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.code = function(){
        console.log(this.name + " is coding");
    }
}

var xiaoming = new Person("xiaoming",10,"man");
console.log(xiaoming);// {name: "xiaoming", age: 10, sex: "man", code: ƒ}
xiaoming.code();// xiaoming is coding   

var xiaoming2 = Object.assign({}, xiaoming, {test1:'demo1', test2:'demo2'}); // 第一个参数是 目标对象,后面的全是源对象,执行完之后返回目标对象
console.log(xiaoming2);// {name: "xiaoming", age: 10, sex: "man", code: ƒ, test1: 'demo1', test2: 'demo2'}
xiaoming2.code();// xiaoming is coding   

运行结果:

Person {

  name: 'xiaoming',

  age: 10,

  sex: 'man',

  code: [Function (anonymous)]

}

xiaoming is coding

{

  name: 'xiaoming',

  age: 10,

  sex: 'man',

  code: [Function (anonymous)],

  test1: 'demo1',

  test2: 'demo2'

}

xiaoming is coding

方式六:使用扩展运算符...添加属性和方法

ES6新增语法,可以将两个对象合并成一个对象。将多个属性合并成1个对象。

function Person(name,age,sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.code = function(){
        console.log(this.name + " is coding");
    }
}

var xiaoming = new Person("xiaoming",10,"man");
console.log(xiaoming);// {name: "xiaoming", age: 10, sex: "man", code: ƒ}
xiaoming.code();// xiaoming is coding   

var xiaoming2 = {...xiaoming, test1:'demo1', test2:'demo2'};
console.log(xiaoming2);// {name: "xiaoming", age: 10, sex: "man", code: ƒ, test1: 'demo1', test2: 'demo2'}
xiaoming2.code();// xiaoming is coding   

运行结果:

Person {

  name: 'xiaoming',

  age: 10,

  sex: 'man',

  code: [Function (anonymous)]

}

xiaoming is coding

{

  name: 'xiaoming',

  age: 10,

  sex: 'man',

  code: [Function (anonymous)],

  test1: 'demo1',

  test2: 'demo2'

}

xiaoming is coding

猜你喜欢

转载自blog.csdn.net/xijinno1/article/details/132353005
今日推荐