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
4
votes
3 answers

How to use Object.defineProperty for getters in Typescript?

I have used an implementation as given below in JavaScript. class Base { installGetters() { Object.defineProperty(this, 'add', { get() { return 5; } }); } } class New extends Base { …
Sayantan Ghosh
  • 368
  • 5
  • 22
4
votes
2 answers

JavaScript try...catch for defineProperty not working

I'd like to know why the error is not raised inside the catch block when i use Object.defineProperty() method with get() and set()? try { var f; Object.defineProperty(window, 'a', { get: function() { return…
poro6
  • 43
  • 5
4
votes
3 answers

Alternative methods for extending object.prototype when using jQuery

Some time ago I tried to extend Object.prototype... I was surprised when later I saw errors in the console which comes from jQuery file. I tried to figured out what is wrong and of course I found information that extending Object.prototype is a…
LJ Wadowski
  • 6,424
  • 11
  • 43
  • 76
3
votes
1 answer

Accessing get/set methods for JavaScript property created using Object.create

What I'd like is the ability to wrap a JavaScript property to modify behavior on get/set. For properties that are values, I can do the following: var obj = { myProperty : 0 }; function notifyOfChange(obj, propertyName) { var propertyValue =…
guy
  • 587
  • 4
  • 13
3
votes
0 answers

Function returning objects with getters and methods

I was refactoring my code and decided to replace ES6-classes with this-independent factory functions. So before the changes I had something like this: class Person { constructor(name) { this.name = name; } get myName() { …
Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40
3
votes
1 answer

How to get asynchronous data from an object's `get()` without returning a Promise

In NodeJS, I have an object like, var scope = { word: "init" }; Using Object.defineProperty as described in MDN I re-write the get() function to be like this, Object.defineProperty(scope, 'word', { get: function() { return Math.random(); …
3
votes
1 answer

Why does my script throw 'Cannot redefine property: i' Error only after minifying?

I have a script that adds methods to an object in order to keep everything in one place cleanly. This works well with the unminified js but once it gets minified it breaks and I don't why. Does anyone have experience with this, that can guide me to…
colecmc
  • 3,133
  • 2
  • 20
  • 33
3
votes
2 answers

Make a property either invokable or not

I want to make a property either invokable or not. So for example: function Test () { var obj = { someString: 'here is text' }; Object.defineProperty(obj, 'string', { get: function() { return obj.someString; }, set:…
georgej
  • 3,041
  • 6
  • 24
  • 46
3
votes
1 answer

Object define property for getter and setter

I'm getting an error on Maximum call stack size for this code. function ValueObject() { } ValueObject.prototype.authentication; Object.defineProperty(ValueObject.prototype, "authentication", { get : function () { return…
user2727195
  • 7,122
  • 17
  • 70
  • 118
3
votes
1 answer

How to transform Object.defineProperty in IE8

I use React, Webpack, Babel to build the project and wish it run in IE8, there is a problem that Object.defineProperty is not supported in IE8. I didn't use this function but npm packages do it, like react-router. I have tried polyfill, es5-shim,…
3
votes
1 answer

Javascript - Difference between defineProperty and directly defining a function on an object

I recently created my own module for node.js for use with the koa module. It's a translation module like koa-i18n. I've studied other koa modules to see how functions/properties are applied to the koa context/request and some of them use the…
supercell
  • 309
  • 1
  • 4
  • 17
3
votes
3 answers

Access value in getter inside defineProperty

can i access the value that i have defined inside a defineProperty call? I want create something like this: Object.defineProperty(this, 'name', { value: 'Caaaarl', get: function() { return ; }, set: function(x) { = x;…
Shutterfly
  • 199
  • 12
3
votes
1 answer

how to loop through object property's created by Object.defineProperty

i have the following object in my program function Player(id) { this.id = id; this.healthid = this.id + "health"; this.displayText = "blah blah"; this.inFight = false; this.currentLocation = 0; this.xp = 0; this.level =…
Math chiller
  • 4,123
  • 6
  • 28
  • 44
3
votes
3 answers

declaring javascript object property with a function not working

I'm making a lame text-based game, and I made an object player like so: var player = { displayText: "you", currentPosition: 0, level: 1, health: function() { return 10 + (this.level * 15) }, strength: function() {…
Math chiller
  • 4,123
  • 6
  • 28
  • 44
3
votes
1 answer

Clarication needed for implementing properties with the revealing module pattern using Html5 getters and setters

I've searched a lot for how to do properties in Javascript. Most all of the revealing module pattern I've seen has exclusively exposed functions, and from experience I know if I expose an object, I'm only really getting a copy of the value right…
klumsy
  • 4,081
  • 5
  • 32
  • 42
1 2
3
10 11