iife

Pass a variable from one IIFE to another

淺唱寂寞╮ 提交于 2019-12-11 01:36:17
问题 I have an index.html file like this and several .js and .css files. What I would like is to make the two .js files communicate with each other, I want to pass variables from one file to another. index.html : <html lang='en'> <head> <meta charset='utf-8'> <script src='https://d3js.org/d3.v5.js' charset='utf-8'></script> <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script> <link href='/css/a.css' rel='stylesheet'/> <link href='/css/b.css' rel='stylesheet'/>

IIFE vs bind() for event/callback function

懵懂的女人 提交于 2019-12-10 19:27:50
问题 Say, for example, I need to register an onclick event that calls another function sayHello() to say hello, with its parameter as a variable available in the current scope. I could use IIFE to inject the variable into the scope of the anonymous function as follows: var currentName = "James"; something.onclick = (function(name) { return function() { sayHello(name); }; })(currentName); However, I could also use a version of function currying via the bind() method as follows: var currentName =

What do I need to convert my Angular2 component to ES6 syntax?

笑着哭i 提交于 2019-12-10 19:22:43
问题 index.js Here is my entry point import * as stylesheet from '../assets/styles/app.scss'; import jQuery from '../node_modules/jquery/dist/jquery'; import $ from '../node_modules/jquery/dist/jquery'; import {bootstrap} from './boot' import {AppComponent} from './app.component' bootstrap(AppComponent); app.component.js This works (function (app) { app.AppComponent = ng.core .Component({ selector: 'my-app', template: '<h1>My First Angular 2 App</h1>' }) .Class({ constructor: function () { } }); }

Return a variable as a property in an IIFE

自作多情 提交于 2019-12-10 15:54:35
问题 I am trying to return a variable set after the initialization of an IIFE as a property. The issue is if I bind the variable directly, I get an empty object. If I bind it through a function, I get my desired result. var Application = (function(){ var localInformation = {}; function init(){ localInformation = _demoApiCall(); } function _demoApiCall(){ // Pretend this method isn't here, and returns a complex object return { name: "Demo" } } function doWork(){ // localInformation is properly

IIFE. TypeError: require(…)(…) is not a function

不问归期 提交于 2019-12-10 10:34:26
问题 Running simple script . Got an error. const fetch = require("node-fetch") const url = "https://www.someurl.com" (async ()=>{ const response = await fetch(url) const data = await response console.log(data) })() ERROR $ node api.js TypeError: require(...)(...) is not a function What am I missing here ? Thank you. 回答1: Automatic Semicolon Insertion(ASI) doesn't work as you expect it to in some cases. IIFEs fall into one of those cases, where the parentheses are concatenated with previous line

Difference between using void vs wrapping in parens for IIFE void function() vs (function())

痴心易碎 提交于 2019-12-09 11:33:07
问题 The common practise for creating modules is to wrap them in parens so you won't leak any variables outside of the module (when concatenating etc). There is also void operator, which evaluates a given expression and returns undefined . (See on MDN) I wonder what is the reason behind preferring wrapping functions in parens instead of using void . Is it historical, is it something related to concatenation, else? I know that you can have problems with concatenation when one of the files has a

How to Sync call in Node.js

北城余情 提交于 2019-12-08 07:29:02
问题 I have following code snippet: var array = [1, 2, 3]; var data = 0; for(var i=0; i<array.length; i++){ asyncFunction(data++); } console.log(data); executeOtherFunction(data); I am expecting value of data as 3 but I see it as 0 due to asyncFunction . How do I call executeOtherFunction when all the asyncFunction calls are done? 回答1: Use async.each: var async = require('async'); var data = 0; var array = [ 1, 2, 3 ]; async.each(array, function(item, done) { asyncFunction(data++, done); },

How to you test an angularjs module defined inside an IIFE with jasmine?

余生颓废 提交于 2019-12-07 07:09:58
问题 how do I test this module on jasmine ? The problem is that it's very difficult to test the $controller because the function is hidden inside a closure, it’s very difficult to test them. In other words, given the module definition below, writing a unit test for MainCtrl seems impossible. (function () { 'use strict'; angular.module('app', []); function MainCtrl() { var mc = this; mc.obj = { val : 50 }; } angular.module('app').controller('MainCtrl', MainCtrl); } () ); and the "typical" jasmine

Immediately Invoked Function Expression: Where to put the parenthesis?

假如想象 提交于 2019-12-07 01:30:17
问题 I've seen IIFE's written: (function() { console.log("do cool stuff"); })(); as well as: (function() { console.log("do more cool stuff"); }()); They seem to work the same in any context I've used them, though in cases I've been told one way is right and the other is wrong, vice versa. Does anyone have any solid reason or logic as to it being written one order over the other? Is there some cases where there could be potentially more going on after the function body closes but before the

IIFE. TypeError: require(…)(…) is not a function

纵饮孤独 提交于 2019-12-06 07:31:52
Running simple script . Got an error. const fetch = require("node-fetch") const url = "https://www.someurl.com" (async ()=>{ const response = await fetch(url) const data = await response console.log(data) })() ERROR $ node api.js TypeError: require(...)(...) is not a function What am I missing here ? Thank you. Automatic Semicolon Insertion(ASI) doesn't work as you expect it to in some cases. IIFE s fall into one of those cases, where the parentheses are concatenated with previous line code. To ameliorate this, just prefix your IIFE's with a semicolon: const fetch = require("node-fetch") const