品易云推流 关闭
文章详情页
文章 > JavaScript > js中对象的两种属性

js中对象的两种属性

js 对象

头像

小妮浅浅

2021-10-26 10:00:334022浏览 · 0收藏 · 0评论

1、对象有两种属性,普通的数据属性和访问器属性。

2、访问器属性本质上是用于获取和设置值的函数(可以拦截、过滤、处理等操作要设置或获取的属性),但从外部代码来看就像传统属性一样。

实例

const user = {
    name: "John",
    surname: "Smith",
    get fullName() {
        return `${this.name} ${this.surname}`;
    },
    set fullName(value) {
        [this.name, this.surname] = value.split(' ');
    },
};
console.log(user.fullName); // John Smith
user.fullName = 'test fullName';
console.log(user.fullName); // test fullName

以上就是js中对象的两种属性,希望对大家有所帮助。更多js学习指路:js教程

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图