iife

Javascript IIFE as an object's property (method)

偶尔善良 提交于 2019-12-24 01:55:55
问题 I'm trying to use an IIFE as a method (which might be wrong). Why ? Because, I'm trying to implement the proxy design pattern. In adobe extendscript, there is an "app" object to access documents, etc, like - var length = app.activeDocument.length; // or some other property Now, I wanted to put a proxy around "app". So I created a proxy object - var AppProxy = { activeDocument: function() { // do stuff...; return app.ActiveDocument; } } But now, this is how I had to access it - var length =

What is the difference between assigning an IIFE's public members to a variable vs returning an object

女生的网名这么多〃 提交于 2019-12-23 17:07:09
问题 I've been looking at a lot of JavaScript code lately and I've seen two different ways of using assigning "public" properties of IIFE's. The first is to create a variable and assign that variable to a property inside of the IIFE like so: var public1; (function(){ var foo= "Foo", bar= "Bar"; public1= { getFoo: function(){ return foo; } }; }()); The second way I see is returning an object from the IIFE like so: var public2 = (function(){ var foo2= "Foo2", bar2= "Bar2"; return { getBar: function(

I have a confusion about IIFE

守給你的承諾、 提交于 2019-12-21 07:10:13
问题 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) })() 回答1: 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

I have a confusion about IIFE

不羁的心 提交于 2019-12-21 07:10:02
问题 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) })() 回答1: 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

How do I import an IIFE-based JavaScript module into an Angular TypeScript app?

牧云@^-^@ 提交于 2019-12-21 04:57:04
问题 So I have a third-party SDK written as an oldschool IIFE based module. In other words it looks something like this: var ThirdPartySDK = (function() { var export = {}; // Add some methods to export return export; })(); You would then be expected to use it by referencing it on the global scope like this: <html> <body> <script src="lib/ThirdPartySDK.js"> <script> ThirdPartySDK.foo(); <\script> <\body> <\html> I could still use it this way of course, but is that really the best practice with

Why does TypeScript pack a class in an IIFE?

若如初见. 提交于 2019-12-21 03:39:59
问题 Here is a TypeScript class: class Greeter { public static what(): string { return "Greater"; } public subject: string; constructor(subject: string) { this.subject = subject; } public greet(): string { return "Hello, " + this.subject; } } It is transpiled to IIFE when TS targets ES5: var Greeter = /** @class */ (function () { function Greeter(subject) { this.subject = subject; } Greeter.what = function () { return "Greater"; }; Greeter.prototype.greet = function () { return "Hello, " + this

declaring a variable twice in IIFE

断了今生、忘了曾经 提交于 2019-12-20 10:25:49
问题 I came through this fun quiz on the internet. console.log((function(x, f = (() => x)){ var x; var y = x; x = 2; return [x, y, f()] })(1)) and the choices were: [2,1,1] [2, undefined, 1] [2, 1, 2] [2, undefined, 2] I picked solution 2 TBH, basing that on that x has been redefined, y was declared and defined with no value, and that f has a different scope hence getting the global x memory spot than function x memory spot. However, I tried it in jsbin.com and I found it was solution 1, while I

What are these patterns in this Backbone TodoMVC example

随声附和 提交于 2019-12-18 09:16:11
问题 Looking into the todomvc backbone codes example. The structure in the js/ fold: ├── app.js ├── collections │ └── todos.js ├── models │ └── todo.js ├── routers │ └── router.js └── views ├── app-view.js └── todo-view.js app.js var app = app || {}; $(function () { 'use strict'; // kick things off by creating the `App` new app.AppView(); }); collections/todos.js var app = app || {}; (function () { 'use strict'; var Todos = Backbone.Collection.extend({ model: app.Todo, app.todos = new Todos(); })(

Why is this grouping operator + function immediately invoked

我是研究僧i 提交于 2019-12-18 04:11:29
问题 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?

Immediately-Invoked Function Expression (IIFE) vs not

大城市里の小女人 提交于 2019-12-17 17:14:05
问题 I see a lot of code like: var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; //"this" is global, because it runs immediately on load. Caller is global myApp.sayGoodbye = function() { console.log("Goodbye"); }; })(); Which causes the anonymous function to execute immediately. But what is the advantage of this, compared to just putting the code inline? var myApp ={}; console.log("Hello"); var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); }; Apparently it