简体   繁体   English

对象字面量构造函数

[英]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 构造函数,所以我确实覆盖了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 oa=3 or with keyword, where o is defined using an object literal?简单来说,任何人都可以a=3不将对象 o 传递给函数或代理或使用oa=3with关键字(其中o是使用对象字面量定义的)的情况下使a=3问题吗?

Just do oa=3;就做oa=3; and console log o :和控制台日志o

 let o = {a:1,b:2}; oa=3; console.log(o);

Currently you are doing oa===3 which will return Boolean value instead of overwriting the value of property a .目前您正在执行oa===3 ,它将返回Boolean值而不是覆盖属性a的值。

You also cannot do oa=3 inside console.log() because oa=3 will return the assigned value which is 3 but it will still change the property a of o to 3 .您也不能在console.log()执行oa=3因为oa=3将返回分配的值3但它仍会将o的属性a更改为3

 let o = { a: 1, b: 2 }; //won't print o but changes oa to 3 console.log(oa = 3);

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通过Object.create

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

question in simpler terms can anyone make a=3 without passing the object o to a function or a proxy?简单来说,任何人都可以在不将对象 o 传递给函数或代理的情况下使 a=3 吗?

  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.那是不可能的,因为如果有可能,您可以破坏每一块 javascript。 And that would be terrible.那将是可怕的。

Oh wait, this is JS...哦等等,这是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 oa holds a reference to the variable a - which unfortunately only works the way you'd like for variables that are passed by reference .如果oa持有对变量a的引用,则可以完成您想要实现的目标 - 不幸的a ,这仅适用于您希望通过引用传递的变量的方式。 Primitive types like String , Number and Boolean though, are passed by value .不过,像StringNumberBoolean这样的原始类型是通过value传递

Check the examples below:检查以下示例:

 var a = 1; var o = { a: a }; a = 2; console.log(oa, 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()这样的对象构造函数也无济于事,因为它确实返回对对象的引用(根据需要通过引用传递),但是为它分配一个新值读取该值仍然返回一个原语(再次通过value传递):

 var a = new Number(1); var o = { a: a.valueOf() }; a = Number(2); console.log(oa, 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(oa[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM