js报错 Cannot both specify accessors and a value or writable attribute


问题来源


let upload_var = {
    "upload_sw":0
}; // 1 为开启可以上传文件 0为关闭不能上传文件
Object.defineProperty(upload_var,'upload_sw', {
    configurable:true,
    value:0,
    set:function(newVal){
        if(newVal == 1){
            let submit_button = getIDD("submit_button") submit_button.className="upload_button"
            console.log('set:'+this._type)
        }else if(newVal == 0){
            let submit_button = getIDD("submit_button")
            submit_button.className="not_submit_button"
        }
    },
    get:function(){
        console.log('get:'+this._upload_sw)
        return this.value
    }
})

解决方法

在使用Object.defineProperty() 定义对象属性的时候,如果设置了 set 或 get, 就不能设置 writable 和 value 中的任何一个

声明:一代明君的小屋|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - js报错 Cannot both specify accessors and a value or writable attribute


欢迎来到我的小屋