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 

运行结果:

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

相关文章

  • Bootstrap按钮组件详解

    Bootstrap按钮组件详解

    按钮组和下拉菜单组件一样,需要依赖于button.js插件才能正常运作。通过本文给大家详细介绍Bootstrap按钮组件,感兴趣的朋友一起学习吧
    2016-04-04
  • JavaScript中检测数据类型的四种方法总结

    JavaScript中检测数据类型的四种方法总结

    这篇文章主要为大家详细介绍了四个JavaScript中检测数据类型的常用方法,文中的示例代码讲解详细,具有一定的参考价值,需要的可以参考一下
    2023-04-04
  • JS设计模式之单例模式(一)

    JS设计模式之单例模式(一)

    这篇文章主要为大家详细介绍了JS设计模式之单例模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • js实现div闪烁原理及实现代码

    js实现div闪烁原理及实现代码

    闪烁的原理是什么呢:其实就一个,display在none与block之间频繁的交替,这样说你明白了么,示例代码如下,希望对大家有所帮助
    2014-06-06
  • 浅谈JavaScript的push(),pop(),concat()方法

    浅谈JavaScript的push(),pop(),concat()方法

    下面小编就为大家带来一篇浅谈JavaScript的push(),pop(),concat()方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 手把手教会你使用redux的入门教程

    手把手教会你使用redux的入门教程

    本文主要介绍了手把手教会你使用redux的入门教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • javascript之querySelector和querySelectorAll使用介绍

    javascript之querySelector和querySelectorAll使用介绍

    其实关于querySelector和querySelectorAll的介绍说明很多。在此主要是做个记录
    2011-12-12
  • JS中input表单隐藏域及其使用方法

    JS中input表单隐藏域及其使用方法

    这篇文章主要介绍了JS中input表单隐藏域及其使用方法讲解,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • JS如何在数组指定位置插入元素

    JS如何在数组指定位置插入元素

    这篇文章主要介绍了JS如何在数组指定位置插入元素,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 兼容firefox的给每一个onClick再附加一个事件

    兼容firefox的给每一个onClick再附加一个事件

    如原来的onClick=="alert("a");",增加后变成onclick="alert("a");return false;"。 如果用脚本实现动态增加上去? 谢谢!
    2008-07-07

最新评论