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

defineProperty descriptor Parameter and Object.prototype dilemma

I am using defineProperty to create an object properties,.Does the descriptor parameter in the function necessarily need to be an object. Here is the code: var config=new Object(); var defineProp = function ( obj, key, value ){ config.value = value;…
1
vote
1 answer

Object.defineProperty in for loop

In my code I have an object, var obj = { item1: { coords: { A: new Point(905.76, 1626.67), //... }, inf: { //... } }, item2: { coords: { A: new…
user7575063
1
vote
3 answers

How to define getters and setters in javascript using object.defineproperty

I have been trying to write getters and setters using object.defineproperty,but could not .I have been trying this example but it throws an error as no firstName property defined.can someone please help me this function person(fName, lName)…
Geeky
  • 7,420
  • 2
  • 24
  • 50
1
vote
1 answer

How do I assign a value using = when a setter is passed?

I am write a compatibility layer over a legacy library function whose internal signature looks like — function legacyLibraryFunction(context) { context.foo.bar = "data" return context } The new system however, doesn't recommend assigning custom…
Shubham Kanodia
  • 6,036
  • 3
  • 32
  • 46
1
vote
2 answers

Adding property to object.prototype using dot notation not working as expected

Here // inherit methods of Date to extend it. var extendDate=Date.prototype; // add year property to Date.prototype, thus to an instance of Date /*extendDate.year={ get: function() { return this.getFullYear(); }, set: function(y) {…
Logan Lee
  • 807
  • 9
  • 21
1
vote
2 answers

How can I define a property on the tab's main window object?

I'm trying to define the property jQuery on the window object from a content script with code in the setter. That way, when the actual jQuery object is defined, I can use it right away. I seem to be unable to get it right, though. The target website…
Protector one
  • 6,926
  • 5
  • 62
  • 86
1
vote
1 answer

Using Object.defineProperty with Angular throws TypeError: Cannot assign to read only property (property) of #
I have an object on my scope, and I want that object to have a few non-enumerable properties, but after setting the enumerable descriptor to false, whenever I try to change the value, I get: "TypeError: Cannot assign to read only property…
Andrew Luhring
  • 1,762
  • 1
  • 16
  • 37
1
vote
1 answer

JS: how to define properties on numeric variables

I've few variables that are of type number (or for that matter strings too). I want add some metadata to these variables. When I try to use Object.defineProperty(myvar, "prop", {/*getter & setter*/}) it gives an error that myvar is not an…
Vivek Athalye
  • 2,974
  • 2
  • 23
  • 32
1
vote
1 answer

Self references in Object.defineProperties()

When adding multiple properties to an object, adding them with Object.defineProperties() usually produces cleaner and more readable code than adding them one by one or in groups. However, if a property references another one,…
balage
  • 38
  • 6
1
vote
1 answer

Unexpected reference giving from a getter method

Here's an object containing a list of strings(valuesDictionnary), these strings are made readable from the outside of the object with a list of getters set as a property of the object(gettersDictionnary). This strange structure is used to make the…
Owakes
  • 11
  • 2
1
vote
1 answer

Redefining .push() with Object.defineProperty that returns array

If I have a property on an object created by calling Object.defineProperty on the prototype of its constructor function that returns an array such as: function Foo() { this._bar = []; } Object.defineProperty(Foo.prototype, 'bar', { get:…
user1569339
  • 683
  • 8
  • 20
1
vote
1 answer

Javascript inheritance of properties defined with Object.defineProperty

I have the following parent class... function Parent(id, name, parameters) { Object.defineProperty(this, "id", { value: id }); Object.defineProperty(this, "name", { value: name, writable: true }); }; and the…
fyaa
  • 646
  • 1
  • 7
  • 25
1
vote
2 answers

How do I redefine a property from a prototype?

How do I remove the property p from the object's prototype? var Test = function() {}; Object.defineProperty(Test.prototype, 'p', { get: function () { return 5; } }); Object.defineProperty(Test.prototype, 'p', { get: function () { return 10;…
stackular
  • 1,431
  • 1
  • 19
  • 41
1
vote
2 answers

How can I add a custom attribute to an HTMLElement, that will default to an empty object on each new instance of the element?

If I add a new property to the prototype of HTMLElement, and have its default value to be '{}' (an empty object): Object.defineProperty(HTMLElement.prototype, 'customObject', { configurable: true, enumerable: true, writeable: true, value: {}…
Yuval A.
  • 5,849
  • 11
  • 51
  • 63
1
vote
1 answer

Object.defineProperty and return values

I'm playing around with a javascript object that defines some getters and setters using the Object.defineProperty method. function User() { var _username; var _id; Object.defineProperty(User, 'id', { get: function() { …
user1491636
  • 2,355
  • 11
  • 44
  • 71