Jump to content

I just got into javascript and here is my understanding of module:

We have some “private data” that we do not want to share or corrupt so we stored them in the inner function. And we tell the enclosing function to return the value of the inner function. Thus we can use those value at our expense without worrying about data corruption. Because now the “value” we are using are references of the real value that is still being protected inside the inner function.

Is this correct?

An example of module:

 

function CoolModule(){
	var something = "cool";
	var another = [1,2,3];

	function doSomething(){
		console.log(something);
	}
	function doAnother(){
		console.log(another.join("!"));
	}

	return{
		doSomething:doSomething,
		doAnother:doAnother
	};
}

var foo=CoolModule();

foo.doSomething();
foo.doAnother();

 

If it is not broken, let's fix till it is. 

Link to comment
https://linustechtips.com/topic/850040-javascript-modules/
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

The thing you're looking for is probably ES2015 Classes, a new feature enabled by most browsers (replacement for functions inside functions and returning them):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Link to comment
https://linustechtips.com/topic/850040-javascript-modules/#findComment-10673709
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×