iife

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

筅森魡賤 提交于 2020-01-09 08:25:29
问题 This question already has answers here : Closed 8 years ago . 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

strange observation on IIFE in node.js (Windows)

喜欢而已 提交于 2019-12-31 04:52:10
问题 Is the below behavior expected for nodejs? It looks buggy to me. If not what am I missing? var abc = function(){ console.log("hello"); } (function(){ console.log("welcome"); })(); I get the below exception TypeError: undefined is not a function at Object.<anonymous> (C:\node\main.js:8:3) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js

Different ways to execute IIFE?

我怕爱的太早我们不能终老 提交于 2019-12-30 11:12:21
问题 Is there any difference between (function (){alert('')} ()) vs (function (){alert('')}) () Both works but when should I use each ? 回答1: The wrapping parentheses are only there to force the parser to parse the construct as a function expression, rather than a function declaration. This is necessary because it's illegal to invoke a function declaration, but legal to invoke a function expression. To that end, it doesn't matter where the invoking parentheses go. It also doesn't matter how you

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

主宰稳场 提交于 2019-12-30 07:50:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . How to using ES6 Arrow function to realize IIFEImmediately-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 = () =

IIFE methods leaks?

你离开我真会死。 提交于 2019-12-29 10:03:45
问题 Probably simple question, hope not duplicate even a bit similar like this one, but is there any rule how to write IIFEs properly for any browser, node.js, etc. ? IIFE examples - second my own created during experiments in IE after watching probably all related Pluralsight videos. Is there any rule, best practise or specificiation how to write methods properly in IIFEs ? In 1st (quite common) example leaks everything except constructor In 2nd not, but not sure why or where to find

Enums in TypeScript: what is the JavaScript code doing?

孤街浪徒 提交于 2019-12-29 07:32:07
问题 The following TypeScript: enum PrimaryColors { Red, Green, Blue }; Produces the following JavaScript: var PrimaryColors; (function (PrimaryColors) { PrimaryColors[PrimaryColors["Red"] = 0] = "Red"; PrimaryColors[PrimaryColors["Green"] = 1] = "Green"; PrimaryColors[PrimaryColors["Blue"] = 2] = "Blue"; })(PrimaryColors || (PrimaryColors = {})); ; I am embarrassed to admit that I don't understand what the JavaScript is doing. The function in parentheses is assigning string values using another

A Javascript function

人盡茶涼 提交于 2019-12-28 04:27:07
问题 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

Manually Invoke IIFE

北慕城南 提交于 2019-12-27 03:35:26
问题 I've got a library (Hubspot Odometer) which I am using in a web application I am developing and it works fine to create and run Odometer style widgets on a page. The trouble is that they are part of a dashboard interface which has panes that are loaded via AJAX. The initial view is not loaded via AJAX so the JavaScript executes fine and the odometers render correctly. When I load a new pane with odometers however they are not rendered correctly nor do they act as they should. The reason for

Javascript IIFE anonymous function [duplicate]

假如想象 提交于 2019-12-25 16:08:32
问题 This question already has answers here : What is the (function() { } )() construct in JavaScript? (23 answers) Closed 5 years ago . I've been trying to understand exactly how IIFEs work in regards to anonymous functions. I understand their use in avoiding global variable collisions and that they create their own local scope. I'm not clear on what happens when an anonymous function like this is called. (function () { var myVar = 'foo'; } )() If this is immediately invoked and it is not

How to separate angularjs files without using global scope

橙三吉。 提交于 2019-12-24 07:12:52
问题 I've seen this post AngularJS best practices for module declaration? But I am still a little confused about the best way to declare and separate angularJS files for modules, controllers, services etc. I understand it is good practice to wrap controllers and services in IIFEs because it prevents them from being used out of scope. And it is good practice to separate your controllers and modules into separate files for organization. My question is, if I have a controller file myCtrl.js (function