javascript - 将 Object.defineProperty 与 Angular 一起使用会引发 TypeError : Cannot assign to read only property (property) of#<Object>

标签 javascript angularjs defineproperty

我的作用域中有一个对象,并且我希望该对象具有一些不可枚举的属性,但是在将可枚举描述符设置为 false 后,每当我尝试更改该值时,我都会得到: “TypeError:无法分配给#的只读属性(property)”

...即使我专门将其可写和可配置描述符设置为 true。为什么会发生这种情况?我可以采取什么措施来解决这个问题? 在非严格模式下,它不会抛出错误,只是不执行任何操作。 根据:MDN,这应该可以正常工作...

代码:

'use strict'
angular.module('app', []);

angular.module('app').controller('TestingCtrl',['$scope', function($scope){
  var that = this;  
  this.content = { contentArea: {} };
  
  function updateIsEmpty(){
    that.content.contentArea.isEmpty = Object.keys(that.content.contentArea).length === 0;
    console.log(that.content.contentArea)
    console.log(Object.keys(that.content.contentArea));
  }
  
  this.addSomethingToCA = function(val){
    that.content.contentArea.something = val;
    updateIsEmpty();
  };
  this.removeSomethingFromCA = function(){
    delete that.content.contentArea.something;
    updateIsEmpty();
  };
  
  (function init(){
    Object.defineProperty(that.content.contentArea, 'isEmpty', {
      enumerable: false
    , value: true
    , configurable: true
    , writeable: true
    });
   
  })();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="TestingCtrl as testingctrl">
    <button ng-if="testingctrl.content.contentArea.isEmpty === true" 
            ng-click="testingctrl.addSomethingToCA('is weird')">add something</button>
    
    <button ng-click="testingctrl.removeSomethingFromCA()">remove something</button>
    <ul>
      <li ng-repeat="(key, val) in testingctrl.content.contentArea">
        key: {{ key }} 
        val: {{ val }}
      </li>
    </ul>
  </div>
</div>

如果运行上面的代码片段,它会抛出: 类型错误:

 Cannot assign to read only property 'isEmpty' of #<Object>
    at updateIsEmpty (http://stacksnippets.net/js:35:38)
    at removeSomethingFromCA (http://stacksnippets.net/js:46:5)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:161:190
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:178:83
    at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:101:273)
    at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:102:48)
    at HTMLButtonElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:178:65)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:27:15
    at Array.forEach (native)
    at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js:7:255)

最佳答案

你有一个错字。属性描述符中的writeable键应该是writable

关于javascript - 将 Object.defineProperty 与 Angular 一起使用会引发 TypeError : Cannot assign to read only property (property) of#<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812352/

相关文章:

javascript - 使用 HTML5 数据属性的 MooTools 事件委托(delegate)

javascript - 为什么我的 jquery .each() 函数不能正常工作?

javascript - Angular 过滤器 : How to pre-filter so angular filters consider only a partial of the whole data object

javascript - ionic ,分离的 Controller ,不工作

javascript - 做一个对外只读的属性,但是我的方法还是可以设置的

javascript - 使用 Javascript 确定屏幕大小的媒体查询

javascript - JQuery 选项卡 UI - 菜单旋转 - OnMouseOver 和 OnMouseOut

AngularJS 服务器 session 和与 NodeJS 后端服务器的身份验证

javascript - 使用 Object.DefineProperty 并访问私有(private)范围内的变量

javascript - DefineProperty描述符参数和Object.prototype困境