首页 > Web开发 > 详细

js 属性getset

时间:2018-10-05 11:22:03      阅读:118      评论:0      收藏:0      [点我收藏+]

属性访问器有多种写法

一、像C#写实体类一样的写法


var attr={
        $x:10,//必须$开头
        get x() {
            return this.$x+1;
        },
        set x(val) {
            this.$x=val+2;
        }
    }
    console.log(attr.x); //11

    attr.x=21;

    console.log(attr.x);//24

二、setAttribute\getAttribute


var shoop=document.getElementsById("psdf‘);
shoop.setAttribute("tittle","a lot of goods")

三、用Objct.defineProperty


var stu={
 _age=20;
editor=1
}
Object.defineProperty(stu,"age",{
    get:function(){
    return this._age;
    },
    set:function(newage){
        this._age=newage;
        this.editor++;
    }
})

stu.age=200;

js 属性getset

原文:https://www.cnblogs.com/wangSOA/p/9744202.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!