【问题标题】:Obj.defineProperty TypeError: : can't redefine non-configurable property 'x_origen'Obj.defineProperty TypeError: : 无法重新定义不可配置的属性“x_origen”
【发布时间】:2015-06-13 01:56:28
【问题描述】:

所以我试图通过添加 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;

        }

【问题讨论】:

标签: javascript


【解决方案1】:

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

        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

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2021-12-15
    • 2011-10-18
    • 2019-02-20
    • 2022-07-12
    • 2019-09-19
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多