iife

Immediately-Invoked Function Expression (IIFE) vs not

半腔热情 提交于 2019-11-28 02:13:18
I see a lot of code like: var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; //"this" is global, because it runs immediately on load. Caller is global myApp.sayGoodbye = function() { console.log("Goodbye"); }; })(); Which causes the anonymous function to execute immediately. But what is the advantage of this, compared to just putting the code inline? var myApp ={}; console.log("Hello"); var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); }; Apparently it's to do with scope of the function, but as the function is anonymous and called by window, it's scope

JavaScript Bang “!” Functions vs Leading Semi-Colon “;” IIFEs

我只是一个虾纸丫 提交于 2019-11-28 00:27:54
Airbnd suggests I do this: !function() { // ... }(); Because: This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. The bang allows me to work around the language's grammar rules: // Evaluated in Chromium 34 console. function(){}(); // => SyntaxError: Unexpected token ( !function(){}(); // => true And when concatenating other modules the bang seems to do the trick: !function(){}();function(){}(); // => SyntaxError: Unexpected token ( !function(){}();!function(){}(); // => true (function(){}());!function

Javascript why wrap a variable or constructor in an IIFE?

冷暖自知 提交于 2019-11-27 23:11:54
I saw something like this today var Visualizer = (function() { function Visualizer() { //... } Visualizer.prototype.function1 = function () { /* ... */ } //... return Visualizer; })(); var viz = new Visualizer(); I don't understand the point of this versus just getting rid of the iife wrapper. There's no point for the specific construct that you show here. The reason to use an IIFE in this type of construct is when you have static data that you need to declare, want to be available to your object, but don't want it to be publicly accessible or interfere with the global namespace or be instance

IIFE context issues

廉价感情. 提交于 2019-11-27 23:09:05
In the following construct: (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!"); } this.show = function(){ alert("This is show function!"); } })(); Why does this refer to window object? Should everything inside IIFE be isolated from global scope? Are x and y functions also properties of window global object? Also, even if I use put var h = ... at the beginning: var h = (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!"); } this.show = function(){ alert("This is show function!"); } })(); this still refers to window object -

A Javascript function

泪湿孤枕 提交于 2019-11-27 15:48:24
Please explain the following way of writing a function in javascript functions : (function (){ // some code })() I understand the fact that because of the trailing braces " () ", the function will execute immediately but but what does enclosing the function in the braces mean? 来源: https://stackoverflow.com/questions/6340874/a-javascript-function

Immediately-Invoked Function Expression (IIFE) In JavaScript - Passing jQuery

梦想的初衷 提交于 2019-11-27 15:39:00
I've got the following code which I know is an IIFE. However, I've never been able to grasp what the (jQuery) and ($) is. I know it's got something to do with passing a reference of jQuery into the IIFE, but could someone explain me clearly the purpose of them? Thank you for you help and time :-) (function ($) { //code })(jQuery); $ is an argument to a function. jQuery is what is being passed as that argument when the function is invoked. Think of it like this: function init($) { // code can use $ here as a shortcut for jQuery // even if $ has a different definition globally or isn't defined

What are the different ways of writing an IIFE? What are their use cases?

╄→гoц情女王★ 提交于 2019-11-27 14:13:04
I have started reading this book. Chapter 2 says that there are different ways to write an IIFE: !function (){}() ~function (){}() +function (){}() -function (){}() new function (){} 1,function (){}() 1&&function (){}() var i=function (){}() The author says: Each manifestation has its own unique qualities and advantages—some with fewer bytes, some safer for concatenation, each valid and each executable. I'm a newbie to JS. I know what an IIFE is, but what do these IIFE forms do exactly? Why do this? Before we get to the list, let's start with "Why do this at all?" The answer is: To keep any

Create a JS class: IIFE vs return prototype

扶醉桌前 提交于 2019-11-27 13:01:29
问题 Let's see two examples in which I'll try to explain what I want to understand. var Car = function(){ // Init class function Car() { }; // Private func/vars var private = { color:'red' }; // Public func/vars Car.prototype = { newColor: function(color) { private.color = color }, getColor: function() { return private.color } }; return Car.prototype; // return with prototype }; var myCar = new Car(); And: var Car = (function(){ // Init class function Car() { }; // Private func/vars var private =

self executing function jquery vs javascript difference

霸气de小男生 提交于 2019-11-27 12:02:06
What are the difference among - First :- (function () { var Book = 'hello'; }()); Second:- (function () { var Book = 'hello'; })(); First and second are similar some how in work.. Third :- (function ($) { var Book = 'hello'; })(jQuery); What pattern I need to use and where in my coding.. Third module pattern I have seen while I was reading a article related to backboneJS. What I understood from Third one "self executing function with the argument “jQuery”" .... Can any please give me some idea about Immediately Invoked Function Expressions (IIFE) . Thanks !! In all cases you are doing an

Namespacing with IIFE in ES6?

左心房为你撑大大i 提交于 2019-11-27 06:48:40
问题 Apparently, ES6 doesn't need namespacing because each file is a separate module. But then, how do I avoid global namespace interference? For example, Babel compiles my scripts/main.js file by merely replacing const with var . var alert = 'This line doesn\'t do anything.' window.alert(alert) A namespace (named ANS below) with an IIFE prevents name collisions: const ANS = (function () { const alert = 'This works' window.alert(alert + '.') return {alert: alert + ' too.'} })() alert(ANS.alert)