[英]Is nesting objects considered good practice?
Is nesting objects and calling them such as var1.var2.func1(); 正在嵌套对象并调用它们,例如var1.var2.func1(); generally ok for performance and coding practice?
性能和编码实践通常还可以吗? There are alternatives but is this method acceptable?
有替代方法,但是这种方法可以接受吗?
function foo(name) {
this.bar = {
xyz: function() {
console.log(name)
}
}
}
var abc = new foo("John");
abc.bar.xyz();
One problem that I can see with the code above is that it violates the Law of Demeter or Principle of least knowledge. 我在上面的代码中看到的一个问题是,它违反了得墨meter耳定律或最低知识原则。
You can read more about this design guideline here: https://en.wikipedia.org/wiki/Law_of_Demeter 您可以在此处阅读有关此设计指南的更多信息: https : //en.wikipedia.org/wiki/Law_of_Demeter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.