Write unit test case of javascript without exposing
Answers
Answered by
0
JavaScript's closures provide an excellent way to make variables ... The immediately invoked function expression returns an object that exposes the bar function globally, ... In other words, given the above code, writing a unit test for bar is ... outside of a function closure without a reference to those variables.
Answered by
0
JavaScript’s closures provide an excellent way to make variables and functions private, keeping them out of the global scope. This is particularly important in the browser because all scripts share the same scope, and it’s quite easy to inadvertently pick a variable or function name used by another library.
The problem, however, is that when functions are hidden inside a closure, it’s very difficult to test them.
Here’s an example:
var myModule = (function() { function foo() { // private function `foo` inside closure return "foo" } return { bar: function() { // public function `bar` returned from closure return "bar" } } }())
The problem, however, is that when functions are hidden inside a closure, it’s very difficult to test them.
Here’s an example:
var myModule = (function() { function foo() { // private function `foo` inside closure return "foo" } return { bar: function() { // public function `bar` returned from closure return "bar" } } }())
Similar questions