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
1
vote
1 answer

what happens if I didn't preserve Object.defineProperty descriptor attributes?

On MDN doc I couldn't understand this part about Object.defineProperty descriptor : Bear in mind that these attributes are not necessarily the descriptor's own properties. Inherited properties will be considered as well. In order to ensure these…
Ayman Morsy
  • 1,279
  • 1
  • 10
  • 28
1
vote
1 answer

Strange things happen when using Object.defineProperty with let or var

Can anyone explain why testVariable has two different ouputs when using let. And why there isn't any runtime error when varibles with the same name are defined in window object? Object.defineProperty(window, 'testVariable', { value: 22 …
James
  • 157
  • 1
  • 6
1
vote
1 answer

Why JavaScript Object's property is already configurable:false, writable can always be changed from true to false without error?

When I read YDKJS book in there say: There's a nuanced exception to be aware of: even if the property is already configurable:false, writable can always be changed from true to false without error, but not back to true if already false. "use…
Murad Sofiyev
  • 790
  • 1
  • 8
  • 25
1
vote
1 answer

JavaScript prototype inheritance with defineProperty

Say I have this "class": function Car() { } Object.defineProperty(Car.prototype, "Make", { get:function() { return this._make; }, set:function(value) { this._make = value; } }); Object.prototype.Drive = function Drive() {…
Mr. TA
  • 5,230
  • 1
  • 28
  • 35
1
vote
2 answers

How to prototype on object instance that is not Object or Function?

Extending Object class is not recommended, so I try to extend an object, for example: var obj = {'a': 1, 'b': 2, 'c': 3}; Here the object literal {'a': 1, 'b': 2, 'c': 3} is same as new Object({'a': 1, 'b': 2, 'c': 3}). I tried obj.prototype =…
user985399
1
vote
1 answer

Why non-enumerable properties don't affect the array built-in methods?

I was playing with the method Object.defineProperty() and the enumerable property of the descriptor argument. On the MDN you can read the next description: enumerable: true if and only if this property shows up during enumeration of the properties…
Shidersz
  • 16,846
  • 2
  • 23
  • 48
1
vote
1 answer

Cross platform module system

Edit: In interest of trying to figure out a solution, I edited the post to explain more clearly of what I'm trying to accomplish. I am trying to re-invent the wheel, with minimal amount of code to create a cross platform async module loading…
Levi Roberts
  • 1,277
  • 3
  • 22
  • 44
1
vote
2 answers

in Typescript, can Object.prototype function return Sub type instance?

I want to write code like class Place { next: Place; get to() : Place { return this; } } let places : Place[]= []; .. places[0].to.next = new Place(); There are many similar classes, so I want define 'to' property to Object.prototype.…
narusas
  • 41
  • 4
1
vote
1 answer

Adding properties in prototype chains using DefineProperty

I found a difference in how JavaScript adds properties to objects. Code example below shows it. var t1 = { x: 1 } var t2 = {} Object.defineProperty(t2, 'y', { value: 2, configurable: false, writable:…
Djorren
  • 21
  • 3
1
vote
1 answer

javascript get set for all properties in an object

I'm trying to build a method that will automatically create get and set functions on all properties within an object. Ideally, when somebody updates the property within an object I want to trigger dirty data to update in the interface. Problem: when…
1
vote
2 answers

Is this imposibble that using defineProperty set function to actually set the property value?

I want to implement a two-way-data-binding (like in Angular or Vue) using vanilla JavaScript. The view to model part I can use add input event listener, and the model to view part, I want use Object.defineProperty's set function. In defineProperty's…
neifnei
  • 95
  • 1
  • 8
1
vote
0 answers

Why can't I use a fat arrow function in Object.define

Why can't I use a fat arrow function in Object.define()? Minimal, Complete, and Verifiable Example Works class Car { constructor(color) { this.color = color; } } Object.defineProperty(Car.prototype, 'getColor', { value: function() { //…
kmiklas
  • 13,085
  • 22
  • 67
  • 103
1
vote
1 answer

Does a getter with Object.defineProperty have access to the instance?

Let's say there is a Person: const Person = function(fname, lname) { this.fname = fname; this.lname = lname; }; And I want to extend its functionality with a "name" getter Object.defineProperty(Person.prototype, 'name', { get: () =>…
craigmichaelmartin
  • 6,091
  • 1
  • 21
  • 25
1
vote
1 answer

{configurable:false} or Object.seal() is not working correctly

I am trying to learn javascript, and have seen that we can play with object's property's attributes. (i mean value, writable, enumerable, configurable). And from what i learned, i have thought changing {configurable: false} would restrict any more…
QnARails
  • 377
  • 1
  • 4
  • 14
1
vote
1 answer

Creating a JavaScript array-like object that can be used like an array and an object

Is it possible to create an object that can be used like an array and an object? The closest I have gotten so far is below which just allows you to assign a value using either an array key or object property: var ALO = { db:…
Craig
  • 2,093
  • 3
  • 28
  • 43