简体   繁体   中英

Is nesting objects considered good practice?

Is nesting objects and calling them such as 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.

You can read more about this design guideline here: https://en.wikipedia.org/wiki/Law_of_Demeter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM