oop - javascript closures vs class instances -


this question more theoretical programming rather practic.

i've been reading closures in javascript. are, put simply, function set of variables accessible function, not copies can made of function. each function has own var a.

however, see working instances of same class, each own members/properties. method of instance can access var a in instance.

do closures accomplish oop can't do? or, @ least, cleaner code, or advantage, when using closures?

sorry put way, see people talking closures if 1 of greatest things javascript has offer... see side effect of creating function scope not vanish. wrong?

edit

as people think question has opinion based answers, i'll reword question discussion becomes concrete:

what can closures cannot done oop javascript offers?

after lot of research, found (or maybe couldn't figure out in past, if i've read it) advantage of closures.

when 1 uses oop in javascript, there no way make private variables (as lone wolf programmer, never needed them). members of object accessible, is, on scope.

but closures make variables accessible code in scope defined. attempt use name of variable outside scope reach other variable (if defined) has same name on scope. , there no way inside scope of these variables get/set them, unless said scope offers get/set methods outside scope. these methods have access variable , can called "outside world". way create private variables getter/setter methods!

another advantage never mess outside variables have same name, stated before, when inside closure's scope.

this called "encapsulation" can achieved closures rather normal javascript oop.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -