大佬教程收集整理的这篇文章主要介绍了访问有序类属性,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对编码真的很陌生,想在这个问题上得到一些帮助:我想以某种有序的方式访问我的类中的对象,直到我访问前三个。
我以为 this[1]
可能意味着 this.x
,但我发现它不能:
class targeTingSolution{
constructor(config){
this.x = config.x
this.y = config.y
this.z = config.z
this.solution = [];
}
target (){
for (var i = 0; i < 3; i++){
this.solution.push(this[i])
}
}
}
// The following lines of code are not required for the solution,but can be
// used by you to test your solution.
const m = new targeTingSolution({
x: 10,y: 15,z: 900
});
console.log(m.target()); // would print "(10,15,900)"
所以我在尝试了很多不同的东西几个小时后在这里找到了这个解决方案
class targeTingSolution{
constructor(config){
this.x = config.x
this.y = config.y
this.z = config.z
this.solution = [];
}
target (){
for (var a in this) {
if(a === 'x' | a === 'y' | a === 'z'){
this.solution.push(this[a])
}
else{
break
}
}
return this.solution;
}
}
// The following lines of code are not required for the solution,900)"
但我想知道是否有更聪明(但更简单)的方法来做到这一点。
我尝试过但我不确定我是否接近的另一个例子是:
class targeTingSolution{
constructor(config){
this.x = config.x
this.y = config.y
this.z = config.z
this.solution = [];
}
target (){
return config.forEach(element => this.solution.push(element));
}
}
// The following lines of code are not required for the solution,900)"
(但它不起作用,因为 config
在
return config.forEach(element => this.solution.push(element));
行
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的访问有序类属性全部内容,希望文章能够帮你解决访问有序类属性所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。