Describe the often cited drawback of a drop-down module template with an example

I read Javascript Design Patterns and then a bunch of SO answers on RMP, and I keep finding that when a flaw is mentioned, this is a quote from a book:

The disadvantage of this template is that if a private function refers to a public function, this public function cannot be overridden if a patch is required. This is due to the fact that a private function will continue to refer to a private implementation, and the template does not apply to public users, but only to functions.

Public elements of an object that reference private variables also obey the no-patch rule.

As a result, modules created using the Revealing Module template may be more fragile than those created using the original module template, so care should be taken during use.

Sorry I'm stupid, but the above explanation just doesn't do it for me. Can someone provide a code-rich visual example of what this flaw means?

+5
source share
1 answer

I think this explains the often cited flaw. Personally, I don’t think this is a big deal if you prefer composition over inheritance, because it just doesn’t appear.

var revealed = (function() { function foo() { return baz(); } function bar() { return "I am the original bar!"; } // private function always calls bar because it in the closure // and it can't see the "instance" variable function baz() { return bar(); } return { foo : foo, bar : bar } })(); var child = Object.create(revealed); child.bar = function() { return "I am the new bar!"; } // we want this to call the new bar but it doesn't console.log(child.foo()); // I am the original bar! 

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1273028/


All Articles