Помогите понять паттерн

Juan2000

Создатель
Регистрация
10 Окт 2008
Сообщения
46
Реакции
1
Although you now have a functioning map, you’re not quite done. You are currently cluttering the global
namespace with all your variables. This might not be a big deal in a simple web page like the one you’re
currently working on. But if you’re constructing a map that will be part of a bigger project, using global
variables can turn out to be a big problem. The problem that can arise is that naming collisions between
conflicting code occur. It’s therefore important to contain your code so that you isolate it from the
outside world.

To make sure that your code doesn’t clutter the global namespace, you will encapsulate it inside a
self-executing anonymous function. The pattern looks like this:

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

It might look a bit odd, but it’s really quite clever. First, an anonymous function looks like function() {
}. By encapsulating it inside a set of parentheses, you return the function to the rest of the code. The
parentheses at the end immediately execute the function, which means that the code inside will run
immediately but will be invisible to all code outside it.
It might be easier to understand if you add an argument to the function:

(function(message) {
alert(message);
})('Hello Google Maps lovers');

Поясните, пожалуйста.
 
1) Анонимная функция, с предотвращением засорения глобального пространства имен, т.е. все переменные что внутри не существуют снаружи;
2) Объявляем функцию и сразу же исполняем ее с передачей строки в качестве параметра
 
Назад
Сверху