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
3
votes
1 answer

Javascript Object.defineProperty produces a TypeError in FireFox 19.02 (MacOS)

Just today I tried to include an older library of mine that uses the Object.defineProperty() method in javascript into an HTML document. I am quite certain that in previous versions of FireFox it worked just fine. However, if I use it now, in FF…
yogibimbi
  • 588
  • 5
  • 17
3
votes
1 answer

JavaScript: assigning a value to a non enumerable property changes it to enumerable?

I think I'm misunderstanding something here. I have an object that contains a property that I wish to be non enumerable. I want to be able to assign values to it from within the object's functions itself, but when I do so, the property becomes…
Pete Gardner
  • 505
  • 1
  • 5
  • 16
3
votes
2 answers

Is there any way to delete a property that is read-only and non-configurable?

I think the answer is "no", other than deleting all references to the containing object and allowing garbage collection to eventually delete all contents of the containing object. Live Example (view log output with console (hit F12 in Chrome,…
Xitalogy
  • 1,592
  • 1
  • 14
  • 17
2
votes
2 answers

Add a JavaScript getter/setter to a native unconfigurable property

I am using special getter/setters to intercept changes made to properties of an object, and it currently works great for any normal user-defined properties. However, I would love if I could employ the same idea to built-in properties like…
devios1
  • 36,899
  • 45
  • 162
  • 260
2
votes
1 answer

Can TypeScript extend an instance's type after calling the instance's method?

I have a JS library that I'm trying to create types for, imagine the following class: class Table { register(...fields) { fields.forEach(field => { Object.defineProperty(this, field, { get: () => { …
Avishay Matayev
  • 119
  • 1
  • 8
2
votes
1 answer

What is the scope of variables when using defineProperty on Javascript class prototypes?

I'm trying to dynamically create getters and setters on a class using defineProperty, but the definition of their function depends on variables above the scope of the defineProperty function call. So when the value of some variable and run…
Paljas
  • 353
  • 3
  • 10
2
votes
1 answer

Stop future object properties from being edited

I'm a JS game dev who's been trying to combat tampermonkey scripts for a while now. I came up with a solution for people hooking into WebSockets where I'd cause the WebSocket to throw an error new WebSocket(0); (0 throws an error due to it being a…
2
votes
1 answer

using 1 and 0 instead of true false for Object.defineProperty

I've seen code on the web like this (simplified): var abc = {}; Object.defineProperty(abc, 'example', { enumerable: 0, configurable: 1, writable: 1, value: function() {} }); where in place of true and…
Kithraya
  • 358
  • 2
  • 10
2
votes
1 answer

TypeScript bug? `this` difference between `get` syntax and `defineProperty`'s `get`

I have got this code: var x = { x1: 5, x2: 7 }; var y = { ...x, _originalX2: x.x2, get x2() { console.log(this.x1); return 9; } }; console.log(y.x2); var z = { ...x, _originalX2:…
SBhojani
  • 499
  • 1
  • 4
  • 19
2
votes
2 answers

Object.defineProperty called on non-object in my react.js application

I was trying to change a file's name before uploading in my react.js application: Here is the approach : onInputChange = (e) =>{ let newFileName='dp'; if(e.target.files.length!==0){ this.state.file=e.target.files[0]; …
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
2
votes
0 answers

Can't assign property that only has getter in superclass

I discovered to my complete surprise today that the following code errors: class Base { get foo() { return 42 } } class Derived extends Base { constructor() { super() console.log(this, Object.getOwnPropertyDescriptor(this, 'foo')) …
Andy
  • 7,885
  • 5
  • 55
  • 61
2
votes
2 answers

javascript - add and delete smart getter programmatically

I'm trying to programmatically add and delete (for caching purposes) getters from an object. I'm adding a getter like this: Object.defineProperty(obj, 'text', { get: getter }) obj.text should only be evaluated the first time it's accessed, and…
Johannes
  • 1,249
  • 3
  • 17
  • 33
2
votes
2 answers

Object.defineProperty sometimes throws

I am playing with ES6 classes and non-writable properties. As an example, I have the following code. I am running it under node version v9.9.0. Crash = class Crash { constructor() { Object.defineProperty(this, 'test', { …
Cliff Stanford
  • 614
  • 7
  • 11
2
votes
3 answers

define property array of objects in Javascript

I have initialized an object that I wish to add to dynamically. Specifically I want to add array of objects to this object. I have tried the below but neither work.. is there a way to do this correctly? The final output should be…
user5640752
  • 101
  • 1
  • 2
  • 13
2
votes
1 answer

JS defineProperty setter doesn't triggered

Q1: Can someone explain how to trigger setter in defineProperty, using it via function by this way? Q2: How to get last key in setter? fiddle is here function test(root) { Object.defineProperty(this, 'subtree', { get: function() { …
user7978965