admin管理员组

文章数量:1297082

In one of his videos (at around 1 min 25 seconds. The clock in the video goes backwards, so it's at -27:45), Douglas Crockford mentions that Javascript closures are a source of enormous expressive power and unlike other power constructs are also secure. He specifically mentions that in Javascript closures constraint scope, which makes them more secure.

Can someone help me with a few examples which show how scoping rules of Javascript closures make them more secure than other languages which have closures. Also are there any other things which make Javascript closures more secure than their counterparts in other languages?

In one of his videos (at around 1 min 25 seconds. The clock in the video goes backwards, so it's at -27:45), Douglas Crockford mentions that Javascript closures are a source of enormous expressive power and unlike other power constructs are also secure. He specifically mentions that in Javascript closures constraint scope, which makes them more secure.

Can someone help me with a few examples which show how scoping rules of Javascript closures make them more secure than other languages which have closures. Also are there any other things which make Javascript closures more secure than their counterparts in other languages?

Share Improve this question asked Feb 14, 2011 at 5:12 ParagParag 12.5k16 gold badges60 silver badges77 bronze badges 1
  • Good question for programmers.stackexchange.... – Nicole Commented Feb 14, 2011 at 5:57
Add a ment  | 

2 Answers 2

Reset to default 10

They are "secure" in the sense that only code within the lexical scope of the closure can directly access the variables of the closures function-scope. I remend reading the Jibbering Closure Notes, in general.

Any "leaked" object may still introduce an data-manipulation/side-effect point. The closures in ECMAScript are no more "secure" than in any other language with similar closure semantics -- in this sense "secure" means "private variable access". On the other hand, some languages already have more controlled visibility modifiers of members (e.g. Java or C# with private/public distinctions).

This is, of course, if one considers JavaScript objects which "merely" rely on prototypes to be "insecure". (If someone uses my code incorrectly, let them -- and if it it breaks, let them burn ;-)

Personally I find Mr. Crockford a fine evangelist -- but not for my religion ;-) One can say all sorts of good thing about X without properly analyzing it in relationship to Y.

Closures give you the ability to encapsulate (hide) data, as desirable with object oriented design.

The ability to effectively define "private" variables and publish a "public" interface makes it less likely that a programmer will misuse the object - i.e. mess with the data value directly and yield 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

本文标签: Why are Javascript closures called secureStack Overflow