162 questions
1
vote
1
answer
86
views
Is it possible to hook a non-configurable property like window.location.hostname, in JavaScript?
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 ...
0
votes
0
answers
37
views
Different exceptions thrown when redefining 'length' property of an array using Object.defineProperty with a literal vs concatenated string
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 ...
0
votes
2
answers
285
views
How to override (monkeypatch) the document and element specific `querySelector` implementations and how to restore each original functionality again?
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....
1
vote
2
answers
137
views
How to correctly implement an own property getter/setter via Object.defineProperty and utilizing class syntax?
it's my code
"use strict";
class Employee {
constructor(firstName, lastName, age, salary, gender) {
this._firstName = firstName;
this._lastName = lastName;
this._age = age;
...
-1
votes
1
answer
37
views
Title: JavaScript this behavior inside an arrow function used in Object.defineProperty [duplicate]
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,
};
...
0
votes
1
answer
43
views
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(...
-3
votes
1
answer
139
views
Where does a local variable get stored, and how can I define a getter/setter for one? [duplicate]
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:
...
1
vote
0
answers
80
views
Object property is unsetting itself
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 ...
2
votes
1
answer
775
views
Can TypeScript extend an instance's type after calling the instance's method?
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, {
...
0
votes
1
answer
683
views
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 ...
0
votes
0
answers
96
views
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", {
...
2
votes
1
answer
48
views
What is the scope of variables when using defineProperty on Javascript class prototypes? [duplicate]
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 ...
2
votes
1
answer
69
views
Stop future object properties from being edited
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 ...
0
votes
1
answer
921
views
JavaScript: How to add a not enumerable method (not a property) in Object.defineProperty
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 ...
0
votes
1
answer
173
views
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 ...
0
votes
1
answer
73
views
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 ...
1
vote
0
answers
5k
views
Why I'm getting "Object.defineProperty called on non-object" when using a binded function as 1st parameter in Object.defineProperty
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: ...
0
votes
1
answer
175
views
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....
4
votes
3
answers
3k
views
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;
}
...
0
votes
1
answer
146
views
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: ...
0
votes
2
answers
88
views
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,...
1
vote
1
answer
65
views
what happens if I didn't preserve Object.defineProperty descriptor attributes?
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 ...
3
votes
0
answers
690
views
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) {
...
0
votes
0
answers
61
views
Read An Object In C# Which Is Created By Object.defineProperty In JavaScript
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");
...
0
votes
1
answer
30
views
when a proto obj be defined by defineProperty then it's hasOwnProperty with the same property would be error ,why?
code like below :
function def(obj, key) {
var val = obj[key];
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter() {
return val
...
2
votes
1
answer
65
views
using 1 and 0 instead of true false for Object.defineProperty
I've seen code on the web like this (simplified):
var abc = {};
Object.defineProperty(abc, 'example', {
enumerable: 0,
configurable: 1,
writable: 1,
value: ...
8
votes
1
answer
2k
views
How to properly bind this to a getter/setter in Javascript
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
},
"...
0
votes
2
answers
196
views
JavaScript: adding method to DOM-Object via prototype - like: Object.defineProperty(Object.prototype, ...) [duplicate]
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....
2
votes
1
answer
125
views
TypeScript bug? `this` difference between `get` syntax and `defineProperty`'s `get`
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 = {...
0
votes
1
answer
163
views
defineproperty on object.prototype does not fire when creating nested objects
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!");} });
...
1
vote
1
answer
777
views
Strange things happen when using Object.defineProperty with let or var
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....
0
votes
1
answer
74
views
Object.definePropery on Array.prototype.push doesnt work
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 ...
2
votes
2
answers
19k
views
Object.defineProperty called on non-object in my react.js application
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){
...
1
vote
1
answer
2k
views
Why JavaScript Object's property is already configurable:false, writable can always be changed from true to false without error?
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, ...
0
votes
0
answers
266
views
limitations of Object.defineProperty and whether Object.prototype has to get involved
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 ...
1
vote
1
answer
450
views
JavaScript prototype inheritance with defineProperty
Say I have this "class":
function Car()
{
}
Object.defineProperty(Car.prototype, "Make",
{
get:function() { return this._make; },
set:function(value) { this._make = value; }
});
Object....
0
votes
3
answers
826
views
How I can set property of object properties?(js)
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.
1
vote
2
answers
107
views
How to prototype on object instance that is not Object or Function?
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': ...
2
votes
0
answers
58
views
Can't assign property that only has getter in superclass
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, ...
1
vote
1
answer
487
views
Why non-enumerable properties don't affect the array built-in methods?
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 ...
0
votes
2
answers
1k
views
Performance difference between Object.defineProperty() and Object.defineProperties()
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 ...
3
votes
1
answer
471
views
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', {
...
1
vote
1
answer
343
views
Cross platform module system
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 ...
3
votes
1
answer
4k
views
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 ...
1
vote
2
answers
2k
views
in Typescript, can Object.prototype function return Sub type instance?
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 ...
5
votes
1
answer
743
views
What happens when invoke Object.defineProperty for a function's prototype?
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 {
...
1
vote
1
answer
89
views
Adding properties in prototype chains using DefineProperty
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: ...
0
votes
0
answers
201
views
Attempting to make Webpack 3 compatible with IE8
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 ...
1
vote
1
answer
882
views
javascript get set for all properties in an object [duplicate]
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 ...
-1
votes
1
answer
46
views
Using toString when setting object prototype to null
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",
...