JS對(duì)象添加屬性和方法的多種方式
方式一:定義對(duì)象時(shí),直接添加屬性和方法
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
運(yùn)行結(jié)果:
Person {
name: 'xiaoming',
age: 10,
sex: 'man',
code: [Function (anonymous)]
}
Xiaoming is coding
方式二:通過(guò)"對(duì)象.屬性名"添加屬性和方法
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();
運(yùn)行結(jié)果:
Fruit { name: 'xihongshi', color: 'red', use: [Function (anonymous)] }
Xihongshi can be to eat
方式三:通過(guò)"對(duì)象['屬性名']"添加屬性和方法
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();
運(yùn)行結(jié)果:
Fruit { name: 'xihongshi', color: 'red', use: [Function (anonymous)] }
Xihongshi can be to eat
方式四:通過(guò) 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
運(yùn)行結(jié)果:
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'}); // 第一個(gè)參數(shù)是 目標(biāo)對(duì)象,后面的全是源對(duì)象,執(zhí)行完之后返回目標(biāo)對(duì)象 console.log(xiaoming2);// {name: "xiaoming", age: 10, sex: "man", code: ?, test1: 'demo1', test2: 'demo2'} xiaoming2.code();// xiaoming is coding
運(yùn)行結(jié)果:
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
方式六:使用擴(kuò)展運(yùn)算符...添加屬性和方法
ES6新增語(yǔ)法,可以將兩個(gè)對(duì)象合并成一個(gè)對(duì)象。將多個(gè)屬性合并成1個(gè)對(duì)象。
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
運(yùn)行結(jié)果:
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
到此這篇關(guān)于JS對(duì)象添加屬性和方法的多種方式的文章就介紹到這了,更多相關(guān)JS對(duì)象添加屬性和方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)改變層大小的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)鼠標(biāo)拖動(dòng)改變層大小的方法,涉及javascript操作鼠標(biāo)事件及樣式的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04不用ajax實(shí)現(xiàn)點(diǎn)擊文字即可編輯的方法
本文給大家分享一段代碼不使用ajax實(shí)現(xiàn)點(diǎn)擊文字即可編輯的方法,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2007-12-12ES6 Promise對(duì)象的應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 Promise對(duì)象的應(yīng)用,結(jié)合實(shí)例形式分析了Promise對(duì)象原理與異步處理相關(guān)操作技巧,需要的朋友可以參考下2019-06-06JS實(shí)現(xiàn)Excel導(dǎo)出功能以及導(dǎo)出亂碼問(wèn)題解決詳解
這篇文章主要為大家詳細(xì)介紹了JavaScript如何調(diào)用后臺(tái)接口實(shí)現(xiàn)Excel導(dǎo)出功能以及導(dǎo)出亂碼問(wèn)題的解決辦法,需要的小伙伴可以參考一下2023-07-07JS生態(tài)系統(tǒng)加速Tailwind?CSS工作原理探究
這篇文章主要為大家介紹了JS?生態(tài)系統(tǒng)加速Tailwind?CSS使用及工作原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Three.js實(shí)現(xiàn)3D機(jī)房效果
這篇文章主要為大家詳細(xì)介紹了Three.js實(shí)現(xiàn)3D機(jī)房效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12