如何在同一个Javascript对象中引用另一个属性的值中的属性?(How to refer a property in a value of another property in the same

编程入门 行业动态 更新时间:2024-10-27 10:20:39
如何在同一个Javascript对象中引用另一个属性的值中的属性?(How to refer a property in a value of another property in the same Javascript Object? [duplicate])

这个问题在这里已有答案:

对象文字声明中的自引用 20个答案

在Gulp.js文件中(但它可以是任何Javascript文件)我在Javascript对象中设置路径:

var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' }

现在,正如您所看到的,我正在尝试将属性“ devDirGlobs ”中的属性“ devDir ”记为“ this.devDir ”。

它不起作用,但它也没有给我任何错误?

任何提示?

在此先感谢您的帮助!

This question already has an answer here:

Self-references in object literals / initializers 23 answers

In a Gulp.js file (but it can be any Javascript file) I set the paths in a Javascript object:

var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' }

Now, as you can see, I'm trying to recall the property "devDir" in the value of the property "devDirGlobs", as "this.devDir".

It doesn't work, but also it doesn't give to me any error?

Any hint?

Thanks in advance for your help!

最满意答案

在声明对象之前,无法访问对象的属性。

但是,您可以使用点语法而不是文字来构造对象。

var paths = {} paths.devDir = 'builds/development/'; paths.devDirGlobs = paths.devDir + '*.html';

或者将常用值移动到共享配置对象中。

var config = { dir: 'builds/development' }; var paths = { devDir : config.dir, devDirGlobs : config.dir + '*.html' };

It's not possible to access the properties of the object before the object has been declared.

However, you could construct the object with dot syntax rather than as a literal.

var paths = {} paths.devDir = 'builds/development/'; paths.devDirGlobs = paths.devDir + '*.html';

Or move common values into a shared config object.

var config = { dir: 'builds/development' }; var paths = { devDir : config.dir, devDirGlobs : config.dir + '*.html' };

更多推荐

本文发布于:2023-08-01 20:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   象中   在同一个   Javascript   duplicate

发布评论

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

>www.elefans.com

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