Search code examples
javascriptobjectprototypeobject-literal

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

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?


Solution

  • 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