JS:根据来自同一元素的不同 属性 值确定 属性 值(使用 .push 方法)
JS: Determine a property value based on a different property value from the same element (using .push method)
我正在尝试将以下元素推送到包含三个属性的对象。我希望其中一个属性被推送以引用与之一起推送的单独 属性。
我想转这段代码(属性 "y"):
if (oncoming[i].x == 600) {
oncoming.push({
pokemon : Math.random() < 0.70 ? kabutops : aerodactyl,
x : cvs.width,
y : runningHeight
});
}
类似这样的东西(同样,关于 属性 "y"):
if (oncoming[i].x == 600) {
oncoming.push({
pokemon : Math.random() < 0.70 ? kabutops : aerodactyl,
x : cvs.width,
y : this.pokemon == aerodactyl ? flyingHeight : runningHeight
});
}
W3 文档 (https://www.w3schools.com/js/js_this.asp) 告诉我,在方法中使用时,"this" 关键字指的是所有者对象。在此示例中,所有者对象 oncoming[i] 的 y 值为 runningHeight。所以我不认为我可以在这种情况下使用 "this"。
我应该采用另一种方法吗?我是否认为 "this" 在这里不合适?感谢您的帮助。
if (oncoming[i].x == 600) {
let newPokemon = Math.random() < 0.70 ? kabutops : aerodactyl
oncoming.push({
pokemon : newPokemon,
x : cvs.width,
y : newPokemon == aerodactyl ? flyingHeight : runningHeight
});
}
我正在尝试将以下元素推送到包含三个属性的对象。我希望其中一个属性被推送以引用与之一起推送的单独 属性。
我想转这段代码(属性 "y"):
if (oncoming[i].x == 600) {
oncoming.push({
pokemon : Math.random() < 0.70 ? kabutops : aerodactyl,
x : cvs.width,
y : runningHeight
});
}
类似这样的东西(同样,关于 属性 "y"):
if (oncoming[i].x == 600) {
oncoming.push({
pokemon : Math.random() < 0.70 ? kabutops : aerodactyl,
x : cvs.width,
y : this.pokemon == aerodactyl ? flyingHeight : runningHeight
});
}
W3 文档 (https://www.w3schools.com/js/js_this.asp) 告诉我,在方法中使用时,"this" 关键字指的是所有者对象。在此示例中,所有者对象 oncoming[i] 的 y 值为 runningHeight。所以我不认为我可以在这种情况下使用 "this"。
我应该采用另一种方法吗?我是否认为 "this" 在这里不合适?感谢您的帮助。
if (oncoming[i].x == 600) {
let newPokemon = Math.random() < 0.70 ? kabutops : aerodactyl
oncoming.push({
pokemon : newPokemon,
x : cvs.width,
y : newPokemon == aerodactyl ? flyingHeight : runningHeight
});
}