defineproperty

Wrong behaviour in Google Chrome Object.defineProperty?

蹲街弑〆低调 提交于 2019-11-30 12:32:19
I am trying to create an object with setters and getters, and this is my code: var Player = function(height){ var _height = height; Object.defineProperty(this, 'height', { enumerable: false , configurable: true , writable: false , get: function(){return _height;} , set: function(val){_height = val;} }); } var myPlayer = new Player(10); Even though the writable property of the defineProperty options is set to false, I get the following error: Invalid property. A property cannot both have accessors and be writable or have a value, #<Object> The same is happening when the writable is set to true

Make a property that is read-only to the outside world, but my methods can still set

我是研究僧i 提交于 2019-11-30 02:08:33
In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from the outside. The .size property must be maintained/updated from some methods which are on the prototype (and should stay on the prototype). My API is already defined by a specification so I can't modify that (I'm working on a polyfill for an already-defined ES6 object). I'm mostly trying to prevent people from shooting themselves in the foot

Wrong behaviour in Google Chrome Object.defineProperty?

蹲街弑〆低调 提交于 2019-11-29 17:38:49
问题 I am trying to create an object with setters and getters, and this is my code: var Player = function(height){ var _height = height; Object.defineProperty(this, 'height', { enumerable: false , configurable: true , writable: false , get: function(){return _height;} , set: function(val){_height = val;} }); } var myPlayer = new Player(10); Even though the writable property of the defineProperty options is set to false, I get the following error: Invalid property. A property cannot both have

Make a property that is read-only to the outside world, but my methods can still set

这一生的挚爱 提交于 2019-11-28 23:42:39
问题 In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from the outside. The .size property must be maintained/updated from some methods which are on the prototype (and should stay on the prototype). My API is already defined by a specification so I can't modify that (I'm working on a polyfill for an already

Breeze and Angular 'ToDo' app does not work with IE 8

萝らか妹 提交于 2019-11-28 09:27:13
I have downloaded the sample 'Breeze 0.83.5' applications from http://www.breezejs.com/documentation/download The Angular 'ToDo' sample does not work with IE8. I have included the following script references above my Breeze script reference. Any idea why this doesn't work? Breeze's Angular support makes use of the 'backingStoreAdapter' which in turn depends on the ES5 Javascript 'defineProperty' method being implemented by the browser. This was not implemented in IE8, and unfortunately, cannot be supplemented via the use of a shim. This is the only adapter that has this limitation, the breeze

Why can I set [enumerability and] writability of unconfigurable property descriptors?

一曲冷凌霜 提交于 2019-11-27 22:31:04
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty states: configurable : True if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false . So, I have a var x = Object.defineProperty({}, "a", { value:true, writable:true, enumerable:true, configurable:false }); Now I can play with x.a = false , for(i in x) etc. But even though the descriptor is should be unconfigurable, I can do Object.defineProperty(x, "a", {writable:true}); // others defaulting to false Object

Why can I set [enumerability and] writability of unconfigurable property descriptors?

拜拜、爱过 提交于 2019-11-27 19:10:58
问题 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty states: configurable : True if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false . So, I have a var x = Object.defineProperty({}, "a", { value:true, writable:true, enumerable:true, configurable:false }); Now I can play with x.a = false , for(i in x) etc. But even though the descriptor is should be

Trigger action on programmatic change to an input value

情到浓时终转凉″ 提交于 2019-11-27 12:04:31
My objective is to observe an input value and trigger a handler when its value gets changed programmatically . I only need it for modern browsers. I have tried many combinations using defineProperty and this is my latest iteration: var myInput=document.getElementById("myInput"); Object.defineProperty(myInput,"value",{ get:function(){ return this.getAttribute("value"); }, set:function(val){ console.log("set"); // handle value change here this.setAttribute("value",val); } }); myInput.value="new value"; // should trigger console.log and handler This seems to do what I expect, but it feels like a

JS defineProperty and prototype

断了今生、忘了曾经 提交于 2019-11-27 10:48:15
As you know we can define getters and setters in JS using defineProperty() . I've been stuck when trying to extend my class using defineProperty() . Here is an example code: I have an array of fields which must be added to a object fields = ["id", "name", "last_login"] Also I have a class which will be modified var User = (function(){ // constructor function User(id, name){ this.id = id this.name = name } return User; })(); And a function which will add fields to the class using defineProperty() var define_fields = function (fields){ fields.forEach(function(field_name){ var value = null Object

how to use javascript Object.defineProperty

微笑、不失礼 提交于 2019-11-27 05:43:48
I looked around for how to use the Object.defineProperty method, but couldn't find anything decent. Someone gave me this snippet of code : Object.defineProperty(player, "health", { get: function () { return 10 + ( player.level * 15 ); } }) But I don't understand it. Mainly, the get is what I can't get (pun intended). How does it work? Jan Turoň Since you asked a similar question , let's take it step by step. It's a bit longer, but it may save you much more time than I have spent on writing this: Property is an OOP feature designed for clean separation of client code. For example, in some e