最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS對象添加屬性和方法的多種方式

 更新時間:2023年08月18日 09:46:40   作者:西晉的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 

運行結(jié)果:

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();

運行結(jié)果:

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();

運行結(jié)果:

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

運行結(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'}); // 第一個參數(shù)是 目標(biāo)對象,后面的全是源對象,執(zhí)行完之后返回目標(biāo)對象
console.log(xiaoming2);// {name: "xiaoming", age: 10, sex: "man", code: ?, test1: 'demo1', test2: 'demo2'}
xiaoming2.code();// xiaoming is coding   

運行結(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

方式六:使用擴展運算符...添加屬性和方法

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   

運行結(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對象添加屬性和方法的多種方式的文章就介紹到這了,更多相關(guān)JS對象添加屬性和方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

东阳市| 延安市| 手游| 安达市| 金溪县| 织金县| 安达市| 腾冲县| 呼玛县| 彝良县| 北辰区| 建瓯市| 道真| 金山区| 兴义市| 武定县| 丰台区| 浑源县| 沁源县| 新巴尔虎右旗| 娱乐| 余江县| 安仁县| 驻马店市| 玉门市| 长宁区| 政和县| 临漳县| 白银市| 白水县| 苍山县| 肇州县| 阜平县| 南部县| 广南县| 潜山县| 桐柏县| 北辰区| 金山区| 镇平县| 肥乡县|