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

Why does my property disapear after Object.defineProperty in decorator?

I have a decorator that casts a string number to a javascript number. Example: "87" -> 87. The code is quite simple: function digit(target: any, key: string) { // property value var _val = this[key]; // property getter var getter = () => …
raph77777
  • 111
  • 1
  • 8
2
votes
2 answers

Typescript decorator and Object.defineProperty weird behavior

I'm trying to implement a decorator that overrides a property (1) and defines a hidden property (2). Assume the following example: function f() { return (target: any, key: string) => { let pKey = '_' + key; // 1. Define hidden…
user5365075
  • 2,094
  • 2
  • 25
  • 42
2
votes
2 answers

How to make a property/method invokable or not?

I want to achieve this functionality: I have an object var obj = {}; I have three properties on that obj, obj.zero & obj.one& obj.binaryString obj.zero & obj.one are methods while obj.binaryString is a string When I chain the properties, I want…
georgej
  • 3,041
  • 6
  • 24
  • 46
2
votes
0 answers

Refer to the current instance inside Object.defineProperty(#value)

I am trying to make the method getColor() non-configurable, but when I tried to refer to the current instance property of the Fruit "class" using this.color it did't work. Seems like this doesn't refer to the Fruitinstance. How can I refer to the…
2
votes
2 answers

Unset and set a new value to a property defined with defineProperty

I define an object property with Object.defineProperty. But then how can I unset it? I tried to unset it with delete foo.bar (where bar is the property), but it seems it doesn't work: var foo = {}; Object.defineProperty(foo, "bar", { get:…
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
2
votes
1 answer

How to define a constructor function that makes objects with getters and setters?

I'm working on creating an object definition to encapsulate several properties. I have 4 requirements for this object: more than one instance can be created at any time only properties defined on the object can be set/get the object must be…
user1491636
  • 2,355
  • 11
  • 44
  • 71
2
votes
1 answer

Object create define property setter

I need to make it so that every time a specific property on my object is changed - it will call a special method on the same object. Example: MyObject.prototype = Object.create({ specialMethod: function() { /* ... */ } }, { someValue: { …
YemSalat
  • 19,986
  • 13
  • 44
  • 51
2
votes
1 answer

What is a use case for applying defineProperties in javascript?

I have a general question on the practical usage of javascript's ecmascript 5 methods. e.g. Object.defineProperties(obj, value, config) to my knowledge javascript is the driver on the front-end web application. There isn't really a lot of usage in…
user2167582
  • 5,986
  • 13
  • 64
  • 121
2
votes
3 answers

Javascript: prototype x defineProperty x what else

I have some small but useful extra methods for JS core objects like Array: Array.prototype.indexOfObject(property, value) This method returns the index of an Object in a Array (of Objects) where property==value, simple and productive for…
Gustavo
  • 1,673
  • 4
  • 24
  • 39
2
votes
2 answers

how to find the set/get functions of a property (Object.defineProperty)

Suppose a property is defined as follows: Object.defineProperty(window, 'prop', { set: setVal, get: getVal, configurable: true }) ; What I would like to do is to redefine window.prop, for example: var ref2setVal = window.prop.set ; var…
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
2
votes
1 answer

Extend setter default object

Like you all know a button is a button... click, up, down, do this, do that. So I wrote some default button behavior "class/object". external default button.js: function Button(parent) { var self = this; this.enabled = true; this.visible…
jOte-
  • 51
  • 4
2
votes
1 answer

What is the use of Object.defineProperty if I can also do it much simpler?

What is the use of Object.defineProperty ... var myObj = {someNum: 123}; Object.defineProperty(myObj, "anotherNum", {value: 456, writable: true, enumerable: true, configurable: true}); alert(myObj.someNum + " " + myObj.anotherNum); ... if I can…
wubbewubbewubbe
  • 711
  • 1
  • 9
  • 20
1
vote
0 answers

Object property is unsetting itself

After setting a uuid4 object property either by the Object.assign or Object.defineProperty methods, the property sometimes unsets itself in Safari without explanation, as in the following minimal reproducible example. I have not observed this issue…
Bryton Beesley
  • 167
  • 2
  • 12
1
vote
1 answer

JavaScript: How to add a not enumerable method (not a property) in Object.defineProperty

I want to add a method (not a property) to Array.prototype, but I dont want it to be enumerable and mess all for(...in...) in third party libraries. //... Example: //... Only push the value if it is not false Array.prototype.pushValue =…
Wolfgang Amadeus
  • 398
  • 3
  • 12
1
vote
0 answers

Why I'm getting "Object.defineProperty called on non-object" when using a binded function as 1st parameter in Object.defineProperty

I'm trying to bind the this keyword of a function and use the Function as with object dot notation (as Functions are also objects). I figured out using the object literal 'get' to do something like: myFunction.whatever (instead of…
Barleby
  • 618
  • 2
  • 9
  • 20