对象文字中的引用变量?

编程入门 行业动态 更新时间:2024-10-24 22:25:54
本文介绍了对象文字中的引用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说我有

myfunc({ var1: 1, var2: 2, })

如果我想拥有一个利用当前未命名对象的值,我该怎么做?

if i want to have a value that makes use of the current unnamed object, how would I do this?

例如,如果我想要

myfunc({ var1: 1, var2: 2, var3: this.var1 + this.var2 })

显然这不起作用。

正确的语法是什么?

推荐答案

不幸的是,这是不可能的。在构造对象文字时,在评估整个文字之前,不存在对该对象的外部引用。在此阶段使用此的唯一方法是使用构造函数:

Unfortunately, that isn't possible. While an object literal is being constructed, no external reference to that object exists until the entire literal is evaluated. The only way to use this at this stage is to use a constructor instead:

function MyObject() { this.var1 = 1; this.var2 = 2; this.var3 = this.var1 + this.var2; } myfunc(new MyObject());

更多推荐

对象文字中的引用变量?

本文发布于:2023-11-02 13:54:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   对象   文字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!