亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS對(duì)象添加屬性和方法的多種方式

 更新時(shí)間:2023年08月18日 09:46:40   作者:西晉的no1  
本文介紹了如何使用JavaScript對(duì)象添加屬性和方法,通過(guò)實(shí)例演示了如何給對(duì)象添加屬性,以及如何在對(duì)象中定義方法,具有一定的參考價(jià)值,感興趣的可以了解一下

方式一:定義對(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)文章

最新評(píng)論