Is this a valid use case for closing javascript?

I looked through all the other (excellent) answers to SO (especially this: How does JavaScript close work? ), But I wanted your feedback on my understanding of the concept.

I understand that one use case is to hide the implementation of private methods from open access.

Another that I think of has it as a factory generator:

<script> function carFactory( make ) { var m = make; return { manufacture: function ( model ) {console.log("A " + m + " " + model + " has been created");} } } toyotaFactory = carFactory("toyota"); hondaFactory = carFactory("honda"); toyotaFactory.manufacture("corolla"); toyotaFactory.manufacture("corolla"); hondaFactory.manufacture("civic"); </script> 

It is output:

 A toyota corolla has been create A toyota corolla has been created A honda civic has been created 

What do you think is its acceptable precedent for closing (i.e. the creation of several plants using the same code base)? Or can I achieve the same using something much better?

Please note that the question of technical implementation of closures and acceptable examples of use in design / application development is less.

Thanks.

+9
javascript closures application-design
source share
4 answers

Yes, storing private variables is a valid use for closing. This allows you to have private access to a variable without making it a public member.

See this link for other examples: http://www.crockford.com/javascript/private.html

+4
source share

If I understand your question correctly, you do not need to save the make private property? If so, then closing is really not necessary, and you can achieve the same functionality using a prototype ...

 function carFactory(model){ this.m = make; } carFactory.prototype.manufacture = function(model){ console.log('A ' + this.m + ' ' + model + ' has been created'); } 

What is associated with performance benefits (reduced memory and increased speed), on this issue .

+1
source share

Closure is an extremely powerful design with many useful applications. The one you used there is actually a "hack." The most common use for closures is when you have implemented some functionality that “does something” in the middle ... And that “something” can be configured to more or less what you need ... For example, The jQuery ajax function can “do something” when the request has an error, or when it succeeds, etc. .... If you did not have closures, you could only pass a “statically defined” function, which is probably , will need to gather the context necessary to accomplish what you want from global variables, or wa m will have to use the same signature for all functions, like what she did in C or C ++, as a function (customData) {} ... Closures allows you to "dynamically build functions" at run time, rethinking the context that there are them when they were created, so you can go to modules that should "do something" almost everything you want is very simple (in the contract with what you would do in c or C ++ or without closing that is ugly, error prone and hack too).

Therefore, whenever you need to "tweak" some user-defined functions inside something, you will use closure ...

0
source share

I tried to come up with a practical example of hiding / encapsulating information, and this is my attempt so far.

 'use strict'; const bank = (accountHolderName, initialDeposit) => { let balance = initialDeposit; const add = (amount) => { balance += amount; } let subtract = (amount) => { balance -= amount } let printBalance = () => { console.log(accountHolderName, "has balance of", balance, "dollars"); } return { credit: add, debit: subtract, balance: printBalance } } let amy = bank("Amy", 1000); amy.balance(); amy.credit(100); amy.balance(); amy.debit(10); amy.balance(); let sam = bank("Sam", 50); sam.balance(); sam.credit(50); sam.balance(); sam.debit(100); sam.balance(); 

After starting, you will get the following output

 Amy has balance of 1000 dollars Amy has balance of 1100 dollars Amy has balance of 1090 dollars Sam has balance of 50 dollars Sam has balance of 100 dollars Sam has balance of 0 dollars 

Benefits?

  • The balance variable is not directly available.
  • Using closures, each subscriber receives his own data. Amy balance does not interfere with Sam data
  • Callers ( Amy , Sam and others) have a nice API to work with, instead of worrying about the internal details of the bank .

Am I missing something?

0
source share

All Articles