Object literal constructor
Object literal constructor
I am looking for a way to modify an object that was defined by a literal without passing it into another function.
example:
let o = a:1,b:2
console.log(o.a===3)
i thought when i define an object using a literal the the Object constructor would be called, so i did override Object.prototype.constructor
but it only gets called when i do new Object(a:1,b:2)
Object.prototype.constructor
new Object(a:1,b:2)
question in simpler terms can anyone make a=3
without passing the object o to a function or a proxy or using o.a=3
or with
keyword, where o
is defined using an object literal?
a=3
o.a=3
with
o
@vlaz making every javascript object observable, without having to pass it to another function
– user8023326
Sep 15 '18 at 9:10
I want Javascript to change a value, but I don't want to specify where that value is. That is basically what you're telling us?
– connexo
Sep 15 '18 at 9:23
@Delta As the question stands it makes no sense and therefore can't be answered. Please expand it and explain the specific requirements you are mentioning. Why can't you pass it to a function? Why can't you just assign a value?
– Daniel Hilgarth
Sep 15 '18 at 9:27
@DanielHilgarth i want to do it implicitly, by overriding a constructor for example, but i can't find a constructor of an object literal
– user8023326
Sep 15 '18 at 9:31
4 Answers
4
It's simply not possible.
There is no constructor of an object literal.
You can create objects in different ways:
Object.create
Also, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
Answering questions where OP calls the commenters/answerers assholes is probably not the kind of reward OP deserves.
– connexo
Sep 15 '18 at 9:46
@connexo: It's not a reward. It's an end to this discussion.
– Daniel Hilgarth
Sep 15 '18 at 9:48
Just do o.a=3;
and console log o
:
o.a=3;
o
let o = a:1,b:2;
o.a=3;
console.log(o);
Currently you are doing o.a===3
which will return Boolean
value instead of overwriting the value of property a
.
o.a===3
Boolean
a
You also cannot do o.a=3
inside console.log()
because o.a=3
will return the assigned value which is 3
but it will still change the property a
of o
to 3
.
o.a=3
console.log()
o.a=3
3
a
o
3
let o =
a: 1,
b: 2
;
//won't print o but changes o.a to 3
console.log(o.a = 3);
come on, jokes aside, i know its a valid answer but i meant without having accessing property after the object was constructed
– user8023326
Sep 15 '18 at 9:08
I don't get what you'd like to acomplish
– Killer Death
Sep 15 '18 at 9:11
question in simpler terms can anyone make a=3 without passing the object o to a function or a proxy?
o.a = 3;
I am looking for a way to modify an object that was defined by a literal without passing it into another function.
Thats impossible, cause if it would be possible you could break every piece of javascript. And that would be terrible.
Oh wait, this is JS...
with( get o() return a: 3 ; , set o(v) )
o = a:1,b:2;
console.log(o.a===3) // true
What you want to achieve could be done if o.a
holds a reference to the variable a
- which unfortunately only works the way you'd like for variables that are passed by reference. Primitive types like String
, Number
and Boolean
though, are passed by value.
o.a
a
String
Number
Boolean
Check the examples below:
var a = 1;
var o = a: a ;
a = 2;
console.log(o.a, a);
It also doesn't help to use an Object constructor like new Number()
because that does return a reference to an object (which is passed by reference as you'd need), but assigning a new value to it reading the value still returns a primitive (which is, again, passed by value):
new Number()
var a = new Number(1);
var o = a: a.valueOf() ;
a = Number(2);
console.log(o.a, a);
If you pass something that is naturally passed by reference, your expectation is fulfilled:
var a = [ 1 ]
var o = a: a
a[0] = 2
console.log(o.a[0])
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
This sounds like an XY problem. What is the specific issue you are trying to solve?
– VLAZ
Sep 15 '18 at 9:09