Questions tagged [defineproperty]

An ECMAScript 5 method that defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

An ECMAScript 5 method that defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

Object.defineProperty(obj, prop, descriptor)
158 questions
7
votes
1 answer

How to properly bind this to a getter/setter in Javascript

Let's say I have a class which stores the properties of its instances in a nested object: this.Properties = { "Position":{ "X": 400, "Y": 100 }, "Colour": "#007fff7f" }; I wanted to define special getters/setters for each of the…
Permille
  • 130
  • 1
  • 8
7
votes
1 answer

Object.defineProperty vs vanilla property

Considering the basic scenario of usage, do foo.bar = 'baz'; and Object.defineProperty(foo, 'bar', { value: 'baz', configurable: true, enumerable: true, writable: true }); behave exactly the same in supported browsers? Can we fall back to…
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
7
votes
4 answers

Access default getter/setter for JavaScript object

JavaScript getters and setters can be overridden for specific properties using Object.defineProperty. Is there some way to access the default getter/setter (i.e. the function used if the getter/setter has not been overwritten)? In my custom setter I…
Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54
7
votes
3 answers

Multiple Parameters for Object.defineProperty Setter Function?

Is it possible to have multiple parameters for Object.defineProperty setter function? E.G. var Obj = function() { var obj = {}; var _joe = 17; Object.defineProperty(obj, "joe", { get: function() { return _joe; }, set: function(newJoe,…
rob-gordon
  • 1,419
  • 3
  • 20
  • 38
7
votes
6 answers

JavaScript: use defineProperty accessor on an array's .length?

I would like (mainly for academic reasons) to be able to set an accessor on an array's length using Object.defineProperty(), so than I can notify for size changes. I am aware of ES6 object observe, and watch.js, but I would like to attempt to do…
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
6
votes
1 answer

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

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…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
6
votes
1 answer

Typescript mis-reference to _this

I tried to define some property for String.Prototype in TypeScript: Object.defineProperty(String.prototype, 'test', { value: () => { console.log("this is a test over text " + this); } }) in javaScript prototypes, this refers to the…
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
6
votes
2 answers

Weird behaviour with 'use strict' and read only properties

On the MDN strict mode reference page it says Any assignment that silently fails in normal code (assignment to a non-writable property, assignment to a getter-only property, assignment to a new property on a non-extensible object) will throw in…
tleef
  • 3,516
  • 1
  • 24
  • 34
6
votes
3 answers

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

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…
Paul
  • 75
  • 4
5
votes
1 answer

What happens when invoke Object.defineProperty for a function's prototype?

var Foo = function(){}; Object.defineProperty(Foo.prototype,'x',{ get(){ return 3; } }); var foo = new Foo(); console.dir(foo); The result I am looking for should be Foo { __proto__:{ constructor: ƒ (), x: 3, …
yegao
  • 51
  • 2
5
votes
1 answer

Is it valid to use Object.defineProperties with symbols?

Let's take the following code: var obj = {}; var x = Symbol(); Object.defineProperties(obj, { [x]: { value: true, writable: true }, "property2": { value: "Hello", writable: false } // etc. etc. }); console.log(obj[x]) Is…
David Michael Gang
  • 7,107
  • 8
  • 53
  • 98
5
votes
1 answer

IE11 error, Object doesn't support property or method 'defineProperty'

Ok peep's so I have this code, but here's a simplified version: _