Why closing javascript is called safe

In one of his videos (for about 1 minute 25 seconds. The clock in the video goes back, so at -27: 45), Douglas Crockford mentions that closing Javascript is a source of tremendous expressive power and, unlike other power structures, is also safe. He specifically mentions that in the area of ​​constraint constraints, Javascript closes, making them more secure.

Can someone help me with a few examples that show how the rules for viewing Javascript closures make them more secure than other languages ​​that have closures. Also, are there other things that make Javascript locks more secure than their counterparts in other languages?

+6
javascript closures
source share
2 answers

They are “protected” in the sense that only code in the lexical area of ​​the closure can directly access variables of the scope function. I recommend reading notes on closing gossip in general.

Any leaked object can still enter a data manipulation point / side effect. Closing in ECMAScript is no more "secure" than in any other language with similar closing semantics - in this sense, "safe" means "access to a private variable". On the other hand, some languages ​​already have more controlled member visibility modifiers (like Java or C # with private / public differences).

This, of course, if you consider JavaScript objects that “just” rely on prototypes as “unsafe”. (If someone misuses my code, let them - and if it breaks, let them burn ;-)

Personally, I find Mr. Crockford a wonderful evangelist, but not for my religion ;-) You can say all kinds of good things about X, without properly analyzing it in relation to Y.

+9
source share

Closing gives you the ability to encapsulate (hide) data, as desired, with an object-oriented design.

The ability to efficiently define "private" variables and publish a "public" interface makes it less likely that the programmer will abuse the object - i.e. clutter with a given value directly and give unexpected side effects.

// returns an object that does not itself possess "var a" // The closure gives indirect access to a, via the public getter and setter. function f() { var a = 1; return { getA: function() { return a; }, setA: function( A ) { a = A; } }; } var MyObj = new f(); alert(MyObj.a); // --> undefined alert(MyObj.getA()); // --> 1 MyObj.setA(5); alert(MyObj.getA()); // --> 5 

EXAMPLE

+2
source share

All Articles