Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
86 views

I would like to know if there is a way to hook a non-configurable property, like window.location.hostname. By this, I mean if it's possible to get an event/do something when a non-configurable ...
Gérard Hazebrouck's user avatar
0 votes
0 answers
37 views

I have encountered an interesting behavior in JavaScript that I am struggling to understand. When I try to redefine the 'length' property of an array using Object.defineProperty, I get different ...
Trustable's user avatar
0 votes
2 answers
285 views

I have a local testing environment, where I want to temporary override querySelector. I know that monkeypatching is bad practice, but in this case this code will only be used locally on developer side....
romand's user avatar
  • 29
1 vote
2 answers
137 views

it's my code "use strict"; class Employee { constructor(firstName, lastName, age, salary, gender) { this._firstName = firstName; this._lastName = lastName; this._age = age; ...
Delfon's user avatar
  • 39
-1 votes
1 answer
37 views

Question: I'm encountering unexpected behavior when using an arrow function as a getter in Object.defineProperty in JavaScript. Here's my code: "use strict"; const obj = { a: 10, }; ...
Kübra Aksu's user avatar
0 votes
1 answer
43 views

Unable to assign to the value of this for a string prototype definition. function testfunction(thisValue) { thisValue = thisValue || this; thisValue = "new test"; console.log(...
Gary's user avatar
  • 2,349
-3 votes
1 answer
139 views

So in JavaScript, I know that when you define a variable in the global scope, like this, let a = 0; console.log(a); // > 0 it's (more or less) the same as defining a property on the window object: ...
Magnogen's user avatar
1 vote
0 answers
80 views

After setting a uuid4 object property either by the Object.assign or Object.defineProperty methods, the property sometimes unsets itself in Safari without explanation, as in the following minimal ...
Bryton Beesley's user avatar
2 votes
1 answer
775 views

I have a JS library that I'm trying to create types for, imagine the following class: class Table { register(...fields) { fields.forEach(field => { Object.defineProperty(this, field, { ...
Avishay Matayev's user avatar
0 votes
1 answer
683 views

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 ...
Bill2022's user avatar
0 votes
0 answers
96 views

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", { ...
TheCat's user avatar
  • 777
2 votes
1 answer
48 views

I'm trying to dynamically create getters and setters on a class using defineProperty, but the definition of their function depends on variables above the scope of the defineProperty function call. So ...
Paljas's user avatar
  • 373
2 votes
1 answer
69 views

I'm a JS game dev who's been trying to combat tampermonkey scripts for a while now. I came up with a solution for people hooking into WebSockets where I'd cause the WebSocket to throw an error new ...
some dumb guy's user avatar
0 votes
1 answer
921 views

I want to add a method (not a property) to Array.prototype, but I dont want it to be enumerable and mess all for(...in...) in third party libraries. //... Example: //... Only push the value if it is ...
Wolfgang Amadeus's user avatar
0 votes
1 answer
173 views

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 ...
CoderBang's user avatar
  • 123
0 votes
1 answer
73 views

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 ...
Parag Diwan's user avatar
1 vote
0 answers
5k views

I'm trying to bind the this keyword of a function and use the Function as with object dot notation (as Functions are also objects). I figured out using the object literal 'get' to do something like: ...
Barleby's user avatar
  • 698
0 votes
1 answer
175 views

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....
Jacob Guirguis's user avatar
4 votes
3 answers
3k views

I have used an implementation as given below in JavaScript. class Base { installGetters() { Object.defineProperty(this, 'add', { get() { return 5; } ...
Sayantan Ghosh's user avatar
0 votes
1 answer
146 views

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: ...
DoktorD's user avatar
  • 15
0 votes
2 answers
88 views

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,...
Omri Attiya's user avatar
  • 4,047
1 vote
1 answer
65 views

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 ...
Ayman Morsy's user avatar
  • 1,459
3 votes
0 answers
690 views

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) { ...
Eugene Barsky's user avatar
0 votes
0 answers
61 views

in my JavaScript code section, an object is created with DefineProperty like this : $("td", tr).each(function (index, td) { var field = $(td).attr("data-field"); ...
Farzad Karimi's user avatar
0 votes
1 answer
30 views

code like below : function def(obj, key) { var val = obj[key]; Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter() { return val ...
cui peng's user avatar
2 votes
1 answer
65 views

I've seen code on the web like this (simplified): var abc = {}; Object.defineProperty(abc, 'example', { enumerable: 0, configurable: 1, writable: 1, value: ...
Kithraya's user avatar
  • 406
8 votes
1 answer
2k views

Let's say I have a class which stores the properties of its instances in a nested object: this.Properties = { "Position":{ "X": 400, "Y": 100 }, "...
Permille's user avatar
  • 220
0 votes
2 answers
196 views

I want to add some own methods via Object.prototype to work easier with my DOM objects. I found this on SO: How do I write an extension method in JavaScript? (SO link) how to use javascript Object....
Froschkoenig84's user avatar
2 votes
1 answer
125 views

I have got this code: var x = { x1: 5, x2: 7 }; var y = { ...x, _originalX2: x.x2, get x2() { console.log(this.x1); return 9; } }; console.log(y.x2); var z = {...
SBhojani's user avatar
  • 529
0 votes
1 answer
163 views

I'm just fiddling around trying to understand how his is supposed to work, but this scenario has me confused. Object.defineProperty(Object.prototype, 'a', {set: function() {console.log("Set!");} }); ...
user1131308's user avatar
1 vote
1 answer
777 views

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....
James's user avatar
  • 169
0 votes
1 answer
74 views

I want to unable the overriding option on Array.prototype.push on my web application. I have tried to do so with Object.defineProperties(Array.prototype, "push", {writable: false}); But got an ...
GMe's user avatar
  • 1,231
2 votes
2 answers
19k views

I was trying to change a file's name before uploading in my react.js application: Here is the approach : onInputChange = (e) =>{ let newFileName='dp'; if(e.target.files.length!==0){ ...
Maifee Ul Asad's user avatar
1 vote
1 answer
2k views

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, ...
Murad Sofiyev's user avatar
0 votes
0 answers
266 views

I want to use the fact the getter of an object property can be overridden. In attempting to find the right syntax, I ran into trouble where using the following syntax works, but the syntax below it ...
Tim Pozza's user avatar
  • 548
1 vote
1 answer
450 views

Say I have this "class": function Car() { } Object.defineProperty(Car.prototype, "Make", { get:function() { return this._make; }, set:function(value) { this._make = value; } }); Object....
Mr. TA's user avatar
  • 5,377
0 votes
3 answers
826 views

I using js and i want set property of object properties var a=42 Object.defineProperty(this,"a",{value:43} )//Error How I can set property of object properties after defining.
Kayrag's user avatar
  • 19
1 vote
2 answers
107 views

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': ...
user avatar
2 votes
0 answers
58 views

I discovered to my complete surprise today that the following code errors: class Base { get foo() { return 42 } } class Derived extends Base { constructor() { super() console.log(this, ...
Andy's user avatar
  • 8,891
1 vote
1 answer
487 views

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 ...
Shidersz's user avatar
  • 17.2k
0 votes
2 answers
1k views

I am looking for a main differences between those two methods. Some sites mentioned readability concerns, but my concern is mainly performance related. Seems like defineProperty() is faster, but I ...
buriedalive's user avatar
3 votes
1 answer
471 views

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', { ...
adelriosantiago's user avatar
1 vote
1 answer
343 views

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 ...
Levi Roberts's user avatar
  • 1,317
3 votes
1 answer
4k views

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 ...
colecmc's user avatar
  • 3,328
1 vote
2 answers
2k views

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 ...
narusas's user avatar
  • 41
5 votes
1 answer
743 views

var Foo = function(){}; Object.defineProperty(Foo.prototype,'x',{ get(){ return 3; } }); var foo = new Foo(); console.dir(foo); The result I am looking for should be Foo { ...
yegao's user avatar
  • 51
1 vote
1 answer
89 views

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: ...
Djorren's user avatar
  • 21
0 votes
0 answers
201 views

Due to some legacy products that require IE8 compatibility mode we must not use Object.defineProperty calls. We are building a single large bundle from TypeScript modules using ES6 syntax. If that ...
Zoltán Tamási's user avatar
1 vote
1 answer
882 views

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 ...
ImagineEverything_Brad's user avatar
-1 votes
1 answer
46 views

If I set the prototype to null, then how come I can still use toString on the object? var nakedObject=Object.create(null,{ name:{ configurable:true, enumerable:true, value:"Hello", ...
akotch's user avatar
  • 215