简体   繁体   English

如何引用兄弟属性用于属性名称声明? [object literal,Javascript,es6]

[英]How to reference a sibling property for use in property name declaration? [object literal, Javascript, es6]

I have an object that has a property named 'type'. 我有一个具有名为'type'的属性的对象。 This type is the name of another property in the object. 此类型是对象中另一个属性的名称。 Can I achieve my desired output without breaking the declaration up into multiple parts by using Object Literal Syntax. 我可以使用Object Literal Syntax在不将声明分成多个部分的情况下实现所需的输出。 Here is what I have, help is appreciated. 这是我所拥有的,感谢帮助。

 // desired output // { // name: 'Tom Ford', // type: 'random-type', // 'random-type': { // title: 'Blah', // amount: 2000 // } // } var thisRefTest = { name: 'Tom Ford', type: getRandomType(), [this.type]: { title: 'Blah', amount: 2000 } } console.log('thisRefTest', thisRefTest) // output: // { // "name": "Tom Ford", // "type": "random-type", // "undefined": { // "title": "Blah", // "amount": 2000 // } // } var funcRefTest = { name: 'Tom Ford', type: getRandomType(), [function () { return this.type; }()]: { title: 'Blah', amount: 2000 } } console.log('funcRefTest', funcRefTest) // output: // { // "name": "Tom Ford", // "type": "random-type", // "undefined": { // "title": "Blah", // "amount": 2000 // } // } function getRandomType(){ return 'random-type'; } 

Only if you will define a variable with a value first 仅当您首先定义具有值的变量时

var type = getRandomType();
var thisRefTest = {
   name: 'Tom Ford',
   type: type,
   [type]: {
     title: 'Blah',
     amount: 2000
   }
};

The thing is that there is no object at the moment of instantiation. 问题在于实例化时没有对象。 Javascript interpreter can't refer to a property in the object, that is not created, while corresponding closing bracket } is not met. Javascript解释器不能引用对象中的属性,即未创建,而不满足相应的右括号}

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

相关问题 如何在 JavaScript ES6 中使用数组解构并分配给对象属性 - How to use array destructuring and assign to an object property in JavaScript ES6 在对象文字中引用嵌套的'sibling'属性 - Reference nested 'sibling'-property in object literal Javascript ES6 - 如何向 object 添加动态属性? - Javascript ES6 - How to add dynamic property to an object? 如何在Javascript ES6中设置类的属性 - How to set property of class in Javascript ES6 ES6计算属性和嵌套模板文字 - ES6 Computed Property & nested template literal 可以在JavaScript对象文字中使用“创建”作为属性名称吗? - Is it okay to use “create” as a property name in a JavaScript object literal? 如何构造对象的属性和属性的属性。 ES6 Javascript - How to destructure an object's property and property's properties. ES6 Javascript 如何在不使用JavaScript ES6中的构造函数的情况下使用对象文字来创建类的实例? - How can I use an object literal to make an instance of a class without useing the constructor in JavaScript ES6? 使用ES6的属性速记来传递带有Typescript的对象并避免在接口名称前加上参数的简化方法? - Simplified way to use ES6's property shorthand to pass an object with typescript and avoid prefacing arguments with interface name? 使用 (ES6) 计算属性名称更新嵌套对象 - update nested object with (ES6) computed property name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM