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
190
votes
10 answers

how to use javascript Object.defineProperty

I looked around for how to use the Object.defineProperty method, but couldn't find anything decent. Someone gave me this snippet of code: Object.defineProperty(player, "health", { get: function () { return 10 + ( player.level * 15 ); …
Math chiller
  • 4,123
  • 6
  • 28
  • 44
51
votes
2 answers

How do I undo a Object.defineProperty call?

Fiddle var Assertion = function() { return { "dummy": "data" }; } Object.defineProperty(Object.prototype, 'should', { set: function(){}, get: function(){ return new Assertion(this); } }); // Insert magic here. // This needs to…
Raynos
  • 166,823
  • 56
  • 351
  • 396
35
votes
6 answers

JS defineProperty and prototype

As you know we can define getters and setters in JS using defineProperty(). I've been stuck when trying to extend my class using defineProperty(). Here is an example code: I have an array of fields which must be added to a object fields = ["id",…
nick.skriabin
  • 873
  • 2
  • 11
  • 27
29
votes
4 answers

How to "override" a defined (get-)property on a prototype?

I have some code which defines a getter (but no setter, if such is relevant) on a prototype. The value returned is correct in 99.99% of the cases; however, the goal is to set the property to evaluate to a different value for a specific object. foo =…
user2864740
  • 60,010
  • 15
  • 145
  • 220
29
votes
7 answers

Trigger action on programmatic change to an input value

My objective is to observe an input value and trigger a handler when its value gets changed programmatically. I only need it for modern browsers. I have tried many combinations using defineProperty and this is my latest iteration: var…
Christophe
  • 27,383
  • 28
  • 97
  • 140
21
votes
3 answers

Object.defineProperty for all browsers?

Asking about Object.defineProperty as demonstrated below: function testComponent(){ var testProperty; Object.defineProperty(this, "testProperty", { get : function() { return testProperty; }, set…
Shane
  • 4,921
  • 5
  • 37
  • 53
20
votes
1 answer

Object.defineProperty or .prototype?

I've seen two different techniques of implementing non-native features in javascript, First is: if (!String.prototype.startsWith) { Object.defineProperty(String.prototype, 'startsWith', { enumerable: false, configurable: false, …
bantya
  • 576
  • 11
  • 23
19
votes
3 answers

How to customize properties in TypeScript

How do I get TypeScript to emit property definitions such as: Object.defineProperties(this, { view: { value: view, enumerable: false, writable: false, configurable: false }, });
Spongman
  • 9,665
  • 8
  • 39
  • 58
16
votes
3 answers

Make a property that is read-only to the outside world, but my methods can still set

In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from…
jfriend00
  • 683,504
  • 96
  • 985
  • 979
15
votes
1 answer

JavaScript version in HTA

Does anyone know what version of JavaScript is used by HTA files. Currently creating some script files - and trying to make use of Object.defineProperty When running as an HTA - it errors stating that Object doesn't support this property or method. …
DarrenNavitas
  • 239
  • 3
  • 11
14
votes
2 answers

Error in accessor property: can't redefine non-configurable property 'status'

I'm trying to define an object and create an accessor property for it. HTML: JavaScript: crudMode = { create: "Create", read: "Read", update: "Update", delete: "Delete", current:…
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
13
votes
2 answers

TypeScript: How to add static methods to built-in classes

Is there anyhow anyway to add some static method to types like Date, String, Array, etc? For example I want to add method today to Date class and in JavaScript I can simply add a property to it or maybe I use Object.defineProperty: Date.today =…
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
13
votes
1 answer

Wrong behaviour in Google Chrome Object.defineProperty?

I am trying to create an object with setters and getters, and this is my code: var Player = function(height){ var _height = height; Object.defineProperty(this, 'height', { enumerable: false , configurable: true , writable:…
Loupax
  • 4,728
  • 6
  • 41
  • 68
9
votes
1 answer

Object.defineProperty() vs Object.prototype.property vs Object.property when to use what?

Can somebody give me a good use case of when to use Object.defineProperty(), Object.prototype.property and Object.property.
dom
  • 509
  • 1
  • 7
  • 13
8
votes
1 answer

javascript defineProperty to make an attribute non enumerable

I'm trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn't work. Is this code correct? function Item() { this.enumerable = "enum"; this.nonEnum = "noEnum"; } Object.defineProperty(Item, "nonEnum", {…
Naigel
  • 9,086
  • 16
  • 65
  • 106
1
2 3
10 11