简体   繁体   English

嵌套对象是否被认为是好的做法?

[英]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.

相关问题 这种类型的 promise 嵌套是好习惯吗? - Is this type of promise nesting good practice? 链接中的tabIndex:0被认为是一种很好的做法,以避免href =“javascript:void(0);”? - Is tabIndex: 0 in links considered a good practice in order to avoid href=“javascript: void(0);”? 在nodejs模块中,自执行函数是否被视为一种好的做法? - Is self executing function considered a good practice in nodejs module? 创建和发送自定义Javascript事件是否被视为一种好习惯? - Is it considered a good practice to create and dispatch custom Javascript events? 使用html的“确认”方法是否被视为良好做法? - Does using html 'confirm' method considered good practice? 将callBacks传递给redux异步操作被认为是一种好习惯吗? - is it considered good practice to pass callBacks to redux async action? Javascript:在函数之间传递大对象或字符串被认为是一种不好的做法 - Javascript: Passing large objects or strings between function considered a bad practice 是否在html中创建对象并让函数在oop中被视为不良做法? - Is creating objects in html and having functions considered bad practice in oop? 将数组或对象存储在div中是一种很好的做法吗? - Is it a GOOD practice to store Arrays or Objects in a div? 良好的做法是在角度中使用全局对象 - Good practice to use global objects in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM