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'}); // 第一個參數(shù)是 目標對象,后面的全是源對象,執(zhí)行完之后返回目標對象 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對象添加屬性和方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
javascript實現(xiàn)鼠標拖動改變層大小的方法
這篇文章主要介紹了javascript實現(xiàn)鼠標拖動改變層大小的方法,涉及javascript操作鼠標事件及樣式的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04JS實現(xiàn)Excel導出功能以及導出亂碼問題解決詳解
這篇文章主要為大家詳細介紹了JavaScript如何調用后臺接口實現(xiàn)Excel導出功能以及導出亂碼問題的解決辦法,需要的小伙伴可以參考一下2023-07-07JS生態(tài)系統(tǒng)加速Tailwind?CSS工作原理探究
這篇文章主要為大家介紹了JS?生態(tài)系統(tǒng)加速Tailwind?CSS使用及工作原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01