iife

Is there any reason to define module.exports using an IIFE?

回眸只為那壹抹淺笑 提交于 2019-11-29 02:18:49
问题 My team doesn't have any experienced JS developers, but we are writing a library in Node and got a suggestion from a real JS developer that "We should make the js more modular - not to pollute the global namespace and to make it more readable to new-comers", and told us to do the following: module.exports = (function(){ return { nameToExpose: functionToExpose ... }; })(); rather than module.exports.nameToExpose = functionToExpose; What's the point of this, if any? The latter does not make any

JQuery best practice, using $(document).ready inside an IIFE?

…衆ロ難τιáo~ 提交于 2019-11-28 16:43:28
I am looking at a piece of code: (function($) { // other code here $(document).ready(function() { // other code here }); })(jQuery); I though the IIFE does the functions of $(document).ready, is this code correct? or can I just remove the $(document).ready and place the code directly inside the IIFE. No, IIFE doesn't execute the code in document ready. 1. Just in IIFE: (function($) { console.log('logs immediately'); })(jQuery); This code runs immediately logs "logs immediately" without document is ready. 2. Within ready: (function($) { $(document).ready(function(){ console.log('logs after

Namespacing with IIFE in ES6?

痞子三分冷 提交于 2019-11-28 12:12:20
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) Adding properties to the namespace ANS is cleaner than adding them to the global namespace, window , or

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

元气小坏坏 提交于 2019-11-28 08:17:44
问题 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. 回答1: 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 = {/* ...

Why use NOT operator on anonymous function call? (a la Knockout 2.1.0) [duplicate]

送分小仙女□ 提交于 2019-11-28 08:08:28
Possible Duplicate: What does the exclamation mark do before the function? If you look at the source code for KnockoutJS 2.1.0 you will see a code structure like this start on line 7: !function(factory) { ... }(factoryDefinition); The not operator causes this expression to evaluate to true rather than undefined , but why bother? This is a concise way to form an immediately executed function expression. Traditionally, people have used these two forms (function(){ }()); // Recommended by Crockford (function(){ })(); // What most people use If you try to just use function(){ }(); // Syntax error

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

只愿长相守 提交于 2019-11-28 06:48:29
问题 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

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

别说谁变了你拦得住时间么 提交于 2019-11-28 05:59:45
问题 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

How to create multiple instances of IIFE Javascript module?

一曲冷凌霜 提交于 2019-11-28 05:29:53
问题 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

Javascript self executing function “is not a function”

狂风中的少年 提交于 2019-11-28 04:44:24
I have: var Init = (function() { my js goes here })(); And my js executes correctly when the page is loaded. I also have: $('form :checkbox').change(function() { Init(); }); But firebug says Init is not a function. It isn't a function. (function() { ... })() evaluates the anonymous function right then . And the result of the evaluation apparently does not return a function-object in this case :-) Consider: f = (function() { return "not a function :(" })() alert(f()) and f = (function() { return function () { return "Yay!" } })() alert(f()) Happy coding :) Here is a function which will "execute

In JavaScript, what is the advantage of !function(){}() over (function () {})()? [duplicate]

荒凉一梦 提交于 2019-11-28 02:54:46
Possible Duplicate: What does the exclamation mark do before the function? I've long used the following for self-executing, anonymous functions in JavaScript: (function () { /* magic happens */ })() Lately, I've started seeing more instances of the following pattern (e.g., in Bootstrap ): !function () { /* presumably the same magic happens */ }() Anyone know what the advantage is of the second pattern? Or, is it just a stylistic preference? Peter Olson These two different techniques have a functional difference as well as a difference in appearance. The potential advantages of one technique