Как вы называете эти шаблоны? В чем разница между ними? Когда вы будете использовать их? Есть ли другие подобные шаблоны?
(function() {
console.log(this); // window
})();
(function x() {
console.log(this); // window
})();
var y = (function() {
console.log(this); // window
})();
var z = function() {
console.log(this); // window
}();
EDIT: Я только что нашел еще два, казалось бы, избыточных способа сделать это, назвав функции в последних двух случаях...
var a = (function foo() {
console.log(this); // window
})();
var b = function bar() {
console.log(this);
}();
EDIT2: Вот еще один шаблон, приведенный ниже в @GraceShao, который делает функцию доступной вне области действия.
(x = function () {
console.log(this); // window
console.log(x); // function x() {}
})();
console.log(x); // function x() {}
// I played with this as well
// by naming the inside function
// and got the following:
(foo = function bar() {
console.log(this); // window
console.log(foo); // function bar() {}
console.log(bar); // function bar() {}
})();
console.log(foo); // function bar() {}
console.log(bar); // undefined