Skip to main content
added 1 characters in body
Source Link
deadalnix
  • 6k
  • 2
  • 33
  • 27

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

EDIT: After some discuss on stackoverflow's chat, I think this anwer ashas to be precised.

The function in the sample code isn't a closure. However, this fucntion can define local variable and nested functions. All nested functions that use these local variables are closures.

This is usefull to share some data across a set of functions without messing up the global scope. This is the most common use of closure in javascript.

Closure are way more powerfull than just sharing some data like this, but let's be realistic, most programmers don't know a thing about functionnal programming. In C# you would have used class or a namespace for these kind of use, but JS does not provide this functionnality.

You can do way more with closure than just protect the global scope, but this is what you'll see in JS source code.

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

EDIT: After some discuss on stackoverflow's chat, I think this anwer as to be precised.

The function in the sample code isn't a closure. However, this fucntion can define local variable and nested functions. All nested functions that use these local variables are closures.

This is usefull to share some data across a set of functions without messing up the global scope. This is the most common use of closure in javascript.

Closure are way more powerfull than just sharing some data like this, but let's be realistic, most programmers don't know a thing about functionnal programming. In C# you would have used class or a namespace for these kind of use, but JS does not provide this functionnality.

You can do way more with closure than just protect the global scope, but this is what you'll see in JS source code.

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

EDIT: After some discuss on stackoverflow's chat, I think this anwer has to be precised.

The function in the sample code isn't a closure. However, this fucntion can define local variable and nested functions. All nested functions that use these local variables are closures.

This is usefull to share some data across a set of functions without messing up the global scope. This is the most common use of closure in javascript.

Closure are way more powerfull than just sharing some data like this, but let's be realistic, most programmers don't know a thing about functionnal programming. In C# you would have used class or a namespace for these kind of use, but JS does not provide this functionnality.

You can do way more with closure than just protect the global scope, but this is what you'll see in JS source code.

added 833 characters in body
Source Link
deadalnix
  • 6k
  • 2
  • 33
  • 27

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

EDIT: After some discuss on stackoverflow's chat, I think this anwer as to be precised.

The function in the sample code isn't a closure. However, this fucntion can define local variable and nested functions. All nested functions that use these local variables are closures.

This is usefull to share some data across a set of functions without messing up the global scope. This is the most common use of closure in javascript.

Closure are way more powerfull than just sharing some data like this, but let's be realistic, most programmers don't know a thing about functionnal programming. In C# you would have used class or a namespace for these kind of use, but JS does not provide this functionnality.

You can do way more with closure than just protect the global scope, but this is what you'll see in JS source code.

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

EDIT: After some discuss on stackoverflow's chat, I think this anwer as to be precised.

The function in the sample code isn't a closure. However, this fucntion can define local variable and nested functions. All nested functions that use these local variables are closures.

This is usefull to share some data across a set of functions without messing up the global scope. This is the most common use of closure in javascript.

Closure are way more powerfull than just sharing some data like this, but let's be realistic, most programmers don't know a thing about functionnal programming. In C# you would have used class or a namespace for these kind of use, but JS does not provide this functionnality.

You can do way more with closure than just protect the global scope, but this is what you'll see in JS source code.

Because javascripjavascript doesn't hashave feature like namespacenamespaces, and you can mess up pretty easily with all sort of globalsglobal objects.

So thisit is important to be able to isolate some code in it'sits own execution environementenvironment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like CC# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous funtionfunction declaration, followed immediatlyimmediately by its execution. Thus, everything defined within the function is impossible to acessaccess from outside, and you will not mess up the global scope. The executuonexecution context of this function will remain alive as long as some code will useuses it, like nested function definiedfunctions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#  . It's not object oriented, it is prototype oriented. This leads to very differntdifferent practices at the end.

Anyway, closures are good, so use them, even in C#  !

Because javascrip doesn't has feature like namespace, and you can mess up pretty easily with all sort of globals objects.

So this is important to be able to isolate some code in it's own execution environement. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous funtion declaration, followed immediatly by its execution. Thus, everything defined within the function is impossible to acess from outside, and you will not mess up the global scope. The executuon context of this function will remain alive as long as some code will use it, like nested function definied within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#  . It's not object oriented, it is prototype oriented. This leads to very differnt practices at the end.

Anyway, closures are good, so use them, even in C#  !

Because javascript doesn't have feature like namespaces, and you can mess up pretty easily with all sort of global objects.

So it is important to be able to isolate some code in its own execution environment. Closure are perfect for that.

This usage of closure doesn't make sense in a language like C# where you have namespaces, classes and so on to isolates code and not putting everything in the global scope.

A very common practice for javascript code is writting it like this :

(function(){ // Some code })(); 

As you can see, this is an anonymous function declaration, followed immediately by its execution. Thus, everything defined within the function is impossible to access from outside, and you will not mess up the global scope. The execution context of this function will remain alive as long as some code uses it, like nested functions defined within this context, that you can pass as callback or whatever.

Javascript is a very different language than C#. It's not object oriented, it is prototype oriented. This leads to very different practices at the end.

Anyway, closures are good, so use them, even in C#!

Source Link
deadalnix
  • 6k
  • 2
  • 33
  • 27
Loading