-4

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})

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?

  • 3
    This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the specific issue you are trying to solve? – VLAZ Sep 15 '18 at 09:09
  • @vlaz making every javascript object observable, without having to pass it to another function –  Sep 15 '18 at 09:10
  • 1
    *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 09: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 09: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 –  Sep 15 '18 at 09:31
  • It's simply not possible. There is no constructor of an object literal. You can create objects in different ways: Via an object literal *or* via a constructor function *or* via Object.create – Daniel Hilgarth Sep 15 '18 at 09:36
  • @DanielHilgarth thank you just wanted to make sure its impossible, i would chose that as an answer. –  Sep 15 '18 at 09:38
  • Just posted it. – Daniel Hilgarth Sep 15 '18 at 09:38

4 Answers4

2

Just do o.a=3; and console log 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.

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.

let o = {
  a: 1,
  b: 2
};
//won't print o but changes o.a to 3
console.log(o.a = 3);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

It's simply not possible.
There is no constructor of an object literal.
You can create objects in different ways:

  • via an object literal or
  • via a constructor function or
  • via Object.create

Also, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

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

 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

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.

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):

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])
connexo
  • 53,704
  • 14
  • 91
  • 128