Javascript 在另一个值中使用一个值?

Javascript 在另一个值中使用一个值?,javascript,object,Javascript,Object,如果我的问题不是100%清楚,请原谅,希望你能理解我的意思 我希望能够在JS中执行类似的操作,我想知道这是否可能: export const type = { link: 'dark-blue', text: `${link} f2` // using the value of the first in the second } 这项工作: const link = 'dark-blue' export const type = { text: link + ' f2

如果我的问题不是100%清楚,请原谅,希望你能理解我的意思

我希望能够在JS中执行类似的操作,我想知道这是否可能:

export const type = {
    link: 'dark-blue',
    text: `${link} f2` // using the value of the first in the second
}
这项工作:

const link = 'dark-blue'

export const type = {
    text: link + ' f2'
}
但问题是,我必须在对象外部分离我的链接变量


有什么想法吗?

您可以在导出对象后对其进行变异:

export const type = {
  link: 'dark-blue'
}
type.text = `${type.link} f2`;

导出对象后,可以对其进行变异:

export const type = {
  link: 'dark-blue'
}
type.text = `${type.link} f2`;

单例模型可以工作,但是您必须执行
type.text()


单例模型可以工作,但是您必须执行
type.text()


我认为您想做的是引用type对象中的
链接
变量,但您不能这样做,因为对象尚未创建

export const type = {
    link: 'dark-blue',
    text: `${link} f2` // using the value of the first in the second
}
因此,更好的方法是:

const type = {
    link: 'dark-blue'
};

type.text = `${type.link} f2`;

export type;

我认为您想做的是引用type对象中的
链接
变量,但您不能这样做,因为对象尚未创建

export const type = {
    link: 'dark-blue',
    text: `${link} f2` // using the value of the first in the second
}
因此,更好的方法是:

const type = {
    link: 'dark-blue'
};

type.text = `${type.link} f2`;

export type;

您可以使用块范围定义
链接
文本
,然后将属性设置为
类型

let型;
{
const link=“深蓝色”,text=`${link}f2`;
类型={link,text};
}

console.log(type)
您可以使用块范围定义
链接
文本
,然后将属性设置为
类型

let型;
{
const link=“深蓝色”,text=`${link}f2`;
类型={link,text};
}

console.log(type)
在计算对象文本之前,对象不存在,因此不能将一个属性设置为另一个属性的值。您可以将
text
更改为一个getter方法,该方法使用
link
属性返回一个值。另外,您的第二个块生成的对象只有一个属性-您不希望它有一个
链接
属性吗?您不能在JSThank@nnnn中做第一件有意义的事情。对象在计算对象文字后才存在,因此您不能将一个属性设置为另一个属性的值。您可以将
text
更改为一个getter方法,该方法使用
link
属性返回一个值。另外,您的第二个块生成一个只有一个属性的对象-您不希望它有一个
链接
属性吗?您不能在jsThank@nnnnnn中做第一件有意义的事情