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
0 answers

defineProperty to array item on Chrome browser

The problem with array defined elements. After clear all data, elements still exist in array, but length of array returns 0 value. When trying to fill array again, we are didn't see, that array is empty. I was trying to clear array different…
1
vote
1 answer

Using Object.defineProperty on a defined property

Consider a pattern like this, where we want to define a nice getter/setter interface for a property to hide some internal validation: var validThings = { 'pretty name a': 3293293, 'pretty name b': 8275850, 'pretty name c':…
Semicolon
  • 6,793
  • 2
  • 30
  • 38
1
vote
1 answer

Weird behaviour of Object.create()

Can somebody explain what is going on here: code sample 1: o2={b:10}; function classCreate(proto,o){ return Object.create(proto,o); } var o1=classCreate({a:o2},{}); console.log(o1.a.b); // prints 10 code sample 2: o2={b:10}; function…
1
vote
1 answer

How to make dynamic getter with defineProperty in JavaScript?

I want to define dynamic getter functions with defineProperty in JavaScript like below. In order to make a readonly function, I want to use defineProperty. This enables me to throw an Exception in the setter function. However the getter function…
Kai Sasaki
  • 667
  • 4
  • 13
1
vote
1 answer

JS getters: does defineProperty replace or complement the older, inline 'get' 'set' syntax?

There seems to be an overlap in functionality between inline, object literal 'get function()' style and Object.defineProperty. MDN docs for get https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get don't mention that inline…
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
1
vote
2 answers

Javascript intercept array set value

Is there a way to intercept or override the array setter? For example: var array = []; array[2] = 10; var myInterceptor = function(index, newVal) { do stuff when the array value at index is changed. } So I want myInterceptor to be called when…
danial
  • 4,058
  • 2
  • 32
  • 39
1
vote
1 answer

Why does the behavior of a property defined by Object.defineProperty change if accessed before being used?

Only tested this in Chrome, my application doesn't need to work in any other browser. For example in the following code (JSFiddle): function A(a) { document.write(this.B); this.A = a; this.B = a; } function A_C(a) { this.A = a; …
CoryG
  • 2,429
  • 3
  • 25
  • 60
0
votes
1 answer

Unable to assign to the value of `this` for a string prototype definition

Unable to assign to the value of this for a string prototype definition. function testfunction(thisValue) { thisValue = thisValue || this; thisValue = "new test"; console.log(thisValue); this = thisValue; // this throws…
Gary
  • 2,293
  • 2
  • 25
  • 47
0
votes
1 answer

Wanting to change a setter of a single input field, how to advance to check if those were changed before and how not to lose the previous addages?

This is a follow up question to Setter for HTMLInputElement.value. If I changed the setter and getter of a single input-element (not on all input elements in general), and later on I want to make another change (for example done by a third party…
0
votes
0 answers

Why does Object.defineProperty make a property invisible when logged?

I'm trying to understand why using Object.defineProperty makes a property not visible when the object is logged in the console. For instance: let foo = {}; Object.defineProperty(foo, "a", { get() { return "a"; } }); Object.defineProperty(foo,…
TheCat
  • 640
  • 1
  • 9
  • 23
0
votes
1 answer

How to import prototype getter from custom library using Object.defineProperty()

I want to create a reusable library for my projects. For this exercise, I've got one main library, and then an extension library for "identity" related functionality. The idea is that I can include the identity module if / when needed, and extend…
CoderBang
  • 123
  • 1
  • 7
0
votes
1 answer

Adding Runtime Property in Object doesn't show in "this" context

Just wondering , why when this is printed on console , doesn't show the dynamic property, which is added using Object.defineProperty Here is a Decorator example. It's working as expected but the success, error property is not shown in this. I guess…
Parag Diwan
  • 159
  • 7
0
votes
1 answer

Property is missing in type but required in type when using Object.defineProperty

I have this validation middleware that I'm trying to write to validate an empty payload when sending requests : const _isMissing = (body: any): boolean => !body; export const isMissing = Object.defineProperty(_isMissing, '_apiGatewayResponse', { …
0
votes
1 answer

this keyword when extending external js objects properties in ts

I'm converting a codebase from js to ts and stumbled upon a conundrum: extending external js object property getters and setters. interface Player { name: string; position: { x: number; y: number }; _posHistory: [{ x: number; y: number…
DoktorD
  • 15
  • 5
0
votes
2 answers

How to get filtered array with defineProperty JS?

I've tried looking for a way of doing it but found none. I want to filter out array items when I'm accessing the array. For example: filter out only negative values let arr = [-1, -2, -4, -5, 8, 9, 10, -7, 5, 7, 8, 4, -12]; let o = { arr:…
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35