iife

Call IIFE From Another Javascript File

家住魔仙堡 提交于 2019-12-06 07:30:34
问题 I have a file bg.js whose contents is simply an IIFE. I'd like to load this file/call the function in from another function in another file. Since the IIFE is anonymous (it is, right?) I can't call it by name. It seems I have to load the entire file so it executes immediately. I've done searching and found many tutorials and documents about what an IIFE is, but nowhere had I seen how to execute one from another scope. How do I call/load a javascript file that is an IIFE from another

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

独自空忆成欢 提交于 2019-12-05 13:47:15
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 test describe('app', function(){ beforeEach(module('app')); it('should create an objet with val 50',

Immediately Invoked Function Expression: Where to put the parenthesis?

﹥>﹥吖頭↗ 提交于 2019-12-05 05:29:32
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 invoking parenthesis come into play, or after but before that final closing parenthesis? I've mostly used

How to concisely assign and immediately invoke a function variable?

旧街凉风 提交于 2019-12-05 01:28:55
问题 The following is a method for defining an anonymous function within a closure, invoke the function, and forget it: (function () { "do stuff"; })(); This is used to maintain a limited scope without adding bulk to the script (IIFE: Immediately-Invoked Function Expression). What if you're hoping to immediately execute a function, while still retaining the function for future use, like the following: var doThing; (doThing = function () { "do stuff"; })(); This works in the browsers I've tested

Testing javascript inside IIFE

删除回忆录丶 提交于 2019-12-04 22:25:50
My team leader wants me to wrap my js code inside an IIFE immediatly-invoked function expression. I can't figure out how to spyOn in my jasmine spec file. How would I spyOn the follow: (function(){ function_1(){...} function_2(){...} }); spyOn(window,'function_1') doesn't work. Passing window into the IIFE doesn't work. Trying both (x=function()... and (function x() ... then spyOn(x,'function_1') doesn't work either. I couldn't find anything on-line about this. @user29998, if you can expose those functions to test via a return block, you can do something as found on this jsbin link: http:/

Calling function defined in an IIFE function from HTML

ε祈祈猫儿з 提交于 2019-12-04 07:00:54
问题 I have a IIFE function in a file called test.js i.e. (function mainIIFE() { "use strict"; var print_name = function(first, last) { console.log(first + " " + last); }; }()); How would I call print_name in an html file. In my head, I have <head> <script type="text/javascript" src="test.js"></script> </head> and <script> new print_name("Bob", "Downs"); </script> later on in my html file. But when I try to run, it's not recognizing the print_name function. 回答1: Since you declare print_name with

I have a confusion about IIFE

只谈情不闲聊 提交于 2019-12-03 23:53:47
I saw the code below that someone posted. I'm confused about what it logs. It logs variable a the function, not 200. Why? var a = 1; (function a() { a = 200; console.log(a) })() CertainPerformance Because the function being immediately invoked is named , and that name cannot be reassigned to refer to something else directly inside the IIFE. Any named function expressions will exhibit this behavior as well. A function expression whose function is named a will mean that a directly inside the function will always refer to the function itself, even if you try to reassign it. You can make the error

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

♀尐吖头ヾ 提交于 2019-12-03 12:43:36
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 missing a semicolon, leading to nasty problems till you notice it. Examples Say, module1.js (notice the

Immediately invoked function expression without using grouping operator

廉价感情. 提交于 2019-12-03 06:47:58
问题 I'm trying to immediately invoke a function without using IIFE pattern (enclosing a function definition inside parentheses). Here I see two scenarios: When a function declaration is invoked immediately: gives SyntaxError . When a function expression is invoked immediately: executes successfully . Example 1: gives SyntaxError //gives `SyntaxError` function() { console.log('Inside the function'); }(); Example 2: Executes without any error // Executes without any error var x = function()

Why can't IIFE/function access “this” in outer function? [duplicate]

老子叫甜甜 提交于 2019-12-02 23:35:57
问题 This question already has answers here : How does the “this” keyword work? (22 answers) Closed 5 years ago . In the following code, var myObject = { foo: "bar", func: function() { var self = this; console.log("outer func: this.foo = " + this.foo); console.log("outer func: self.foo = " + self.foo); (function() { console.log("inner func: this.foo = " + this.foo); console.log("inner func: self.foo = " + self.foo); }()); } }; myObject.func(); It prints this in the console: outer func: this.foo =