iife

How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))? [closed]

倖福魔咒の 提交于 2019-12-01 02:01:34
How to using ES6 Arrow function to realize IIFE Immediately-Invoked Function Expression ? Here is my demo codes, and it had tested passed! // ES 6 + IIFE (() => { let b = false; console.log(`b === ${b}!`); const print = `print()`; if(window.print){ b = true; console.log(`b === ${b}!`); } let x = () => { if(b){ console.log(`Your browser support ${print} method.`); }else{ alert(`Your browser does not support ${print} method.`); console.log(`Your browser does not support ${print} method.`); }; } x(); })(); const dcs = `IIFE: Douglas Crockford's style`; // ES 5 + IIFE is OK (function(){ alert(

Using Named Immediately-Invoked Function Expression (IIFE) instead of comments

你说的曾经没有我的故事 提交于 2019-11-30 12:06:50
What are the pros and cons of utilizing Named IIFEs within JS code to describe and group related code? I've been using this "pattern" to lend structure to my more procedural code that gets executed only in one place. Example (function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }()); I find this preferable to both: // hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); since over time the comment may get separated from the code and its not immediately obvious

Should I use IIFE or window onload to initialize?

余生颓废 提交于 2019-11-30 09:41:57
Both of the following code snippets worked: Using IIFE in js file: (function initialize() { txtInput = document.getElementById('txtInput'); txtResult = document.getElementById('txtResult'); txtInput.value = "0"; txtResult.value = "0"; }()); Calling initialize() on window load event in html file: window.addEventListener('load', initialize, false); Is one a better approach than other; in terms of performance or otherwise? As it stands right now, I am leaning more towards adding event listener to the window object, because it is more readable. anddoutoi It depends when you want the code to run.

Using Named Immediately-Invoked Function Expression (IIFE) instead of comments

家住魔仙堡 提交于 2019-11-29 18:28:30
问题 What are the pros and cons of utilizing Named IIFEs within JS code to describe and group related code? I've been using this "pattern" to lend structure to my more procedural code that gets executed only in one place. Example (function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }()); I find this preferable to both: // hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo')

Immediately invoked function expression throws “object is not a function”

ぐ巨炮叔叔 提交于 2019-11-29 15:07:24
I'm defining various modules in a Javascript file: var module = {/* ... */} (function(){ console.log('Invoked'); })() However the IIFE throws an error: > TypeError: object is not a function I tried just copy and pasting the IIFE code and there is no issue. The module definition needs a semicolon at the end of the declaration: var module = {/* ... */}; // <======= Semicolon! (function(){ console.log('Invoked'); })() Without it Javascript is trying to call the object: var module = {/* ... */}(function(){console.log('Invoked');})() Or shortened: var module = {/* ... */}() You'd get the same

Should I use IIFE or window onload to initialize?

旧巷老猫 提交于 2019-11-29 14:54:15
问题 Both of the following code snippets worked: Using IIFE in js file: (function initialize() { txtInput = document.getElementById('txtInput'); txtResult = document.getElementById('txtResult'); txtInput.value = "0"; txtResult.value = "0"; }()); Calling initialize() on window load event in html file: window.addEventListener('load', initialize, false); Is one a better approach than other; in terms of performance or otherwise? As it stands right now, I am leaning more towards adding event listener

How does an IIFE's being called immediately prevent it from polluting global scope?

喜你入骨 提交于 2019-11-29 12:28:16
In a Udacity lesson on immediately invoked function expressions (regarding the provided code snippet) it says: The function that is being returned closes over (i.e., captures) the hi variable. This allows myFunction to maintain a private, mutable state that cannot be accessed outside the function! What's more: because the function expressed is called immediately, the IIFE wraps up the code nicely so that we don't pollute the global scope . I'm strugggling to understand what calling the anonymous function immediately has to do with prevent the variable hi from "polluting the global scope," and

Create a class with IIFE that isn't a reference?

拟墨画扇 提交于 2019-11-29 12:03:01
I'm new to JavaScript and I'm trying to wrap my head around creating "classes" with private data and public functions. I've been told Immediately Invoked Function Expressions (IIFE) accomplish this but when I "instantiate" new objects from the class they reference the private data instead of holding their own. Some of this is borrowed from Create a JS class: IIFE vs return prototype For example, a simple Car "class": var Car = (function() { var body = { color: 'red' }; Car.prototype.newColor = function(color) { body.color = color; }; Car.prototype.getColor = function() { return body.color; };

How to create multiple instances of IIFE Javascript module?

本小妞迷上赌 提交于 2019-11-29 11:52:50
I'm dealing with a huge javascript codebase that I'm trying to reorganize. I'm not really an expert and I just started studying good javascript coding practices. So, one thing I'm trying to do is to divide all the code in modules. In this particular case I'm trying to create a module that would help me to optimize video embeds. I would like to pass the module an id and receive some html code or an image out of it. I'm not putting the whole code here, but it's enough for the example: var videoIframe = (function($) { 'use strict'; var id, setVideoId = function(videoId) { id = videoId; console

Why is this grouping operator + function immediately invoked

老子叫甜甜 提交于 2019-11-29 04:04:58
I'am studying the behaviour of Immediatly Invoked Function Expressions (IIFE) and while doing that I encounterd the following situation. (function () { document.write("bar"); }) (function () { document.write("foo"); }()); I thought that the first is just a grouping operator with a function expression inside without calling it. The second is a grouping operator as well with a function expression but now with the call of that function. What I find strange is that both are invoked, why is that? (function () { document.write("bar"); }) var x = 1; (function () { document.write("foo"); }()); When I