目录
  • 方式一:定义对象时,直接添加属性和方法
  • 方式二:通过"对象.属性名"添加属性和方法
  • 方式三:通过"对象['属性名']"添加属性和方法
  • 方式四:通过 prototype (原型)添加属性和方法
  • 方式五:使用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 

运行结果:

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对象添加属性和方法内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!

您可能感兴趣的文章:

  • JS实现给对象动态添加属性的方法
  • JS动态给对象添加属性和值的实现方法
  • Javascript创建自定义对象 创建Object实例添加属性和方法