Obj.defineProperty TypeError ::无法重新定义非可配置属性'x_origen'

时间:2015-04-08 07:59:16

标签: javascript

所以我试图通过添加aumento来增加属性,因为我正在使用obj.defineProperty。但是我收到错误:  TypeError ::无法重新定义非可配置属性'x_origen' 我该如何解决?

var aument = 0;
            function aumento(){ 
            aument += 2.5;
            }
            setInterval(aumento, 20)

        function crear_clase_ladrillo (x_origen_in, y_origen_in, x_final_in, y_final_in, mi_estado, mi_velocidad, mi_id){

            this.nueva_x_origen = this.x_origen + this.velocidad;
            this.y_origen = y_origen_in;
            this.x_final = x_final_in;
            this.nueva_x_final = this.x_final + this.velocidad;
            this.y_final = y_final_in;         
            this.estado      = mi_estado;
            this.velocidad   = mi_velocidad;
            this.id_elemento = mi_id;
            Object.defineProperty(this, 'x_origen', {
                get: function () { return x_origen_in + aument; }
              });

            this.DESPLAZAR_LADRILLO  = desplazar_ladrillo;
            this.F0         = f0;
            this.F2         = f2;
            this.crear_ladrillo = crear_ladrillo;
            this.obtener_x_origen_ladrillo = obtener_x_origen_ladrillo;
            this.obtener_y_origen_ladrillo = obtener_y_origen_ladrillo;
            this.obtener_x_final_ladrillo = obtener_x_final_ladrillo;
            this.obtener_y_final_ladrillo = obtener_y_final_ladrillo;

        }

1 个答案:

答案 0 :(得分:0)

Object.defineProperty()有几个默认值,均为false。您需要明确地将其设为可配置值:

        Object.defineProperty(this, 'x_origen', {
            get: function () { return x_origen_in + aument; },
            configurable: true
            enumerable: true
          });

有关详细信息,请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Description

相关问题