http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li

에서 참조

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this.

In Javascript, you can nest functions. So, the following is legal:

function outerFunction() { function innerFunction() { // code } }

Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:

var globalVariable; function someFunction() { var localVariable; }

Correspondingly:

function globalFunction() { var localFunction1 = function() { //I'm anonymous! But localFunction1 is a reference to me! }; function localFunction2() { //I'm named! } }

In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.

What you’re doing when you write (function() { ... code ... })(), is you’re making the code inside a function literal (meaning the whole “object” is actually a function). After that, you’re self-invoking the function (the final ()). So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:

(function() { var private_var; function private_function() { //code } })()

The neat thing is that you can also define things inside and expose it to the outside world so (an example of namespacing so you can basically create your own library/plugin):

var myPlugin = (function() { var private_var; function private_function() { } return { public_function1: function() { }, public_function2: function() { } } })()

Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:

EDIT

I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:

(function(jQ) { ... code ... })(jQuery) 

So what you’re doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you’re self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). This is because everything you define inside is effectively inside the function. So if you need to know about anything from the outside world, you need to pass it in

http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li

에서 참조

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this.

In Javascript, you can nest functions. So, the following is legal:

function outerFunction() { function innerFunction() { // code } }

Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:

var globalVariable; function someFunction() { var localVariable; }

Correspondingly:

function globalFunction() { var localFunction1 = function() { //I'm anonymous! But localFunction1 is a reference to me! }; function localFunction2() { //I'm named! } }

In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.

What you’re doing when you write (function() { ... code ... })(), is you’re making the code inside a function literal (meaning the whole “object” is actually a function). After that, you’re self-invoking the function (the final ()). So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:

(function() { var private_var; function private_function() { //code } })()

The neat thing is that you can also define things inside and expose it to the outside world so (an example of namespacing so you can basically create your own library/plugin):

var myPlugin = (function() { var private_var; function private_function() { } return { public_function1: function() { }, public_function2: function() { } } })()

Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:

EDIT

I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:

(function(jQ) { ... code ... })(jQuery) 

So what you’re doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you’re self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). This is because everything you define inside is effectively inside the function. So if you need to know about anything from the outside world, you need to pass it in

http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li

에서 참조

It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this.

In Javascript, you can nest functions. So, the following is legal:

function outerFunction() { function innerFunction() { // code } }

Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:

var globalVariable; function someFunction() { var localVariable; }

Correspondingly:

function globalFunction() { var localFunction1 = function() { //I'm anonymous! But localFunction1 is a reference to me! }; function localFunction2() { //I'm named! } }

In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.

What you’re doing when you write (function() { ... code ... })(), is you’re making the code inside a function literal (meaning the whole “object” is actually a function). After that, you’re self-invoking the function (the final ()). So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:

(function() { var private_var; function private_function() { //code } })()

The neat thing is that you can also define things inside and expose it to the outside world so (an example of namespacing so you can basically create your own library/plugin):

var myPlugin = (function() { var private_var; function private_function() { } return { public_function1: function() { }, public_function2: function() { } } })()

Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:

EDIT

I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:

(function(jQ) { ... code ... })(jQuery) 

So what you’re doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you’re self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). This is because everything you define inside is effectively inside the function. So if you need to know about anything from the outside world, you need to pass it in

http://www.quirksmode.org/js/events_advanced.html

에서 참조

Advanced event registration models

See section 7C of the book.

The W3C model is supported by Mozilla, Safari, Konqueror, and iCab.

The Microsoft model is supported by Explorer 5+ on Windows.

Opera supports both models.

On this page I explain the two advanced event registration models: W3C’s and Microsoft’s. Since neither is cross–browser supported, their use is, for the moment, deprecated.

W3C and Microsoft have both developed their own event registration model to replace Netscape’s traditional model. Though I’m not impressed by the Microsoft model, W3C’s is very good, except for one crucial vagueness. Unfortunately few browsers support it at the moment.

W3C

W3C’s DOM Level 2 Event specification pays careful attention to the problems of the traditional model. It offers a simple way to register as many event handlers as you like for the same event on one element.

The key to the W3C event registration model is the method addEventListener(). You give it three arguments: the event type, the function to be executed and a boolean (true or false) that I’ll explain later on. To register our well known doSomething() function to the onclick of an element you do:

element.addEventListener('click',doSomething,false)

The beauty of this model is that we can add as many event listeners as we want to the element. If we take our example modules from the traditional model page, we’d register them both as follows:

element.addEventListener('click',startDragDrop,false)

element.addEventListener('click',spyOnUser,false)

and both event handlers fire when the user clicks on the element. Please note that the W3C model does not state which event handler is fired first, so you cannot assume startDragDrop() is executed before spyOnUser().

To remove an event handler, use the removeEventListener() method. You can choose which handler to remove, so

element.removeEventListener('click',spyOnUser,false)

removes the second event handler but leaves the first one untouched. Beautiful functionality, and it exactly solves the problem with the traditional model we noted before.

Anonymous functions

You can also use anonymous functions in the W3C model:

element.addEventListener('click',function () {

	this.style.backgroundColor = '#cc0000'

},false)

true or false

As to the true or false that is the last argument of addEventListener, it is meant to state whether the event handler should be executed in the capturing or in the bubbling phase. If you’re not certain whether you want capturing or bubbling, use false (bubbling).

this

In JavaScript the this keyword always refers to the “owner” of a function. In the case of event handlers it is very useful if this refers to the HTML element the event is handled by, so that you have easy access to it.

Unfortunately the this keyword, though very powerful, is hard to use if you don’t know exactly how it works. I discuss its use on another page.

In the W3C model it works the same as in the traditional model: it refers to the HTML element currently handling the event.

element.addEventListener('click',doSomething,false);

another_element.addEventListener('click',doSomething,false);



function doSomething() {

	this.style.backgroundColor = '#cc0000';

}

If you register doSomething() as the click event handler of any HTML element, that element gets a red background whenever the user clicks on it.

Which event handlers are registered?

One problem of the current implementation of W3C’s event registration model is that you can’t find out if any event handlers are already registered to an element. In the traditional model you could do:

alert(element.onclick)

and you see the function that’s registered to it, or undefined if nothing is registered. Only in its very recent DOM Level 3 Events W3C adds an eventListenerList to store a list of event handlers that are currently registered on an element. This functionality is not yet supported by any browser, it’s too new. However, the problem has been addressed.

Fortunately removeEventListener() doesn’t give any errors if the event listener you want to remove has not been added to the element, so when in doubt you can always use removeEventListener().

Microsoft

Microsoft, too, has developed an event registration model. It looks similar to W3C’s, but has some important drawbacks.

To register an event handler, attach it to the element:

element.attachEvent('onclick',doSomething)

or, if you need two event handlers:

element.attachEvent('onclick',startDragDrop)

element.attachEvent('onclick',spyOnUser)

Removing one is simple, too:

element.detachEvent('onclick',spyOnUser)

Drawbacks

When compared to the W3C model, the Microsoft model has two important drawbacks:

  1. Events always bubble, no capturing possibility.
  2. The event handling function is referenced, not copied, so the this keyword always refers to the window and is completely useless.

The result of these two weaknesses is that when an event bubbles up it is impossible to know which HTML element currently handles the event. I explain this problem more fully on the Event order page.

Since the Microsoft event adding model is only supported by Explorer 5 and higher on Windows, it cannot be used for cross–browser scripts. But even for Explorer–on–Windows only applications it’s best not to use it, since the bubbling problem can be quite nasty in complex applications.

Continue

If you wish to go through all event pages in order, you should now continue with the Event accessing page