有没有办法在JavaScript中使用以下内容?
var foo = {
a: 5,
b: 6,
c: this.a + this.b // Doesn't work
};
Run Code Online (Sandbox Code Playgroud)
在当前形式中,此代码显然会抛出引用错误,因为this没有引用foo.但是,是有什么办法对早些时候宣布的其他属性在对象文本的属性值依赖?
首先看一下以下的JavaScript对象
var settings = {
user:"someuser",
password:"password",
country:"Country",
birthplace:country
}
Run Code Online (Sandbox Code Playgroud)
我想设置birthplace相同的值country,所以我把对象值country放在前面,birthplace但它对我不起作用,我也用过this.country但仍然失败了.我的问题是如何访问对象内对象的属性.
一些用户沉迷于询问"你想做什么或发送你的脚本等"这些ppls的答案很简单"我想访问对象中的对象属性",上面提到了脚本.
任何帮助将不胜感激 :)
问候
我想在StyleSheet.create的参数对象中重用样式,该怎么做?
看代码:
const styles = StyleSheet.create({
style1: {
color: "red",
backgroundColor: "white"
},
style2: {
width: 100,
...style1
}
})
Run Code Online (Sandbox Code Playgroud)
最初,我想style2继承 allcolor和backgroundColorfrom style1,并扩展 1 个名为width. 所以我确实尝试了上面的代码,但是它得到了一个错误“style1 未声明”。我意识到了这个问题,所以我替换...style1为...this.style1,错误消失了,但它没有按我预期的那样工作。我想知道为什么并尝试在 javascript 中运行此代码段来测试行为:
const styles = StyleSheet.create({
style1: {
color: "red",
backgroundColor: "white"
},
style2: {
width: 100,
...style1
}
})
Run Code Online (Sandbox Code Playgroud)
它得出了结果undefined,事实并非如此styles.style1。我再次意识到这个问题,用 替换inherit: this.style1后inherit: styles.style1,javascript 代码段有效,但在 React Native 代码中 …
javascript ecmascript-6 react-native react-native-stylesheet