javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > JS对象添加属性和方法

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

作者:西晋的no1

本文介绍了如何使用JavaScript对象添加属性和方法,通过实例演示了如何给对象添加属性,以及如何在对象中定义方法,具有一定的参考价值,感兴趣的可以了解一下

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

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

到此这篇关于JS对象添加属性和方法的多种方式的文章就介绍到这了,更多相关JS对象添加属性和方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文