开发者社区 问答 正文

关于JavaScript中对象读取属性的问题

 Object.defineProperties(book , {
        _year: {
            value: 2004      
        },
        edition: {
            value: 1
        },

        year: {
            get: function(){
                return this._year;
            },
            set: function(newValue){
                if(newValue > 2004){
                    this._year = newValue;
                    this.edition += newValue - 2004;
                }
            }
        }
    });

代码如上,为啥在get属性的方法里面返回的是一个undefined呢?

展开
收起
小旋风柴进 2016-03-20 09:31:28 1980 分享 版权
1 条回答
写回答
取消 提交回答
  • 试试把year去掉,get、set和_year同级试试?
    这种处理一般都在后台进行,前台很少写这么复杂的

     _year: {
                value: 2004      
            },
            edition: {
                value: 1
            },
            get: function(){
                return this._year.value;
            },
            set: function(newValue){
                if(newValue > 2004){
                    this._year.value = newValue;
                    this.edition.value += newValue - 2004;
                }
            }
    2019-07-17 19:08:37
    赞同 展开评论