iife

Javascript why wrap a variable or constructor in an IIFE?

一个人想着一个人 提交于 2019-12-17 16:28:57
问题 I saw something like this today var Visualizer = (function() { function Visualizer() { //... } Visualizer.prototype.function1 = function () { /* ... */ } //... return Visualizer; })(); var viz = new Visualizer(); I don't understand the point of this versus just getting rid of the iife wrapper. 回答1: There's no point for the specific construct that you show here. The reason to use an IIFE in this type of construct is when you have static data that you need to declare, want to be available to

What is this JavaScript pattern called and why is it used?

梦想的初衷 提交于 2019-12-17 01:39:31
问题 I'm studying THREE.js and noticed a pattern where functions are defined like so: var foo = ( function () { var bar = new Bar(); return function ( ) { //actual logic using bar from above. //return result; }; }()); (Example see raycast method here). The normal variation of such a method would look like this: var foo = function () { var bar = new Bar(); //actual logic. //return result; }; Comparing the first version to the normal variation, the first seems to differ in that: It assigns the

JavaScript IIFE

我的梦境 提交于 2019-12-13 03:43:57
问题 I'll admit I'm pretty green when it comes to JavaScript and it doesn't help that every time I think I'm getting it some weird curve ball throws me right off. I've got a js file something like this: (function (myiife, $) { var myArrayOfThings = []; myiife.myFunction = function (id) { var cachedThing = myiife.getFromArray(id); if (cachedThing === null) { // get from server } else { // do something with cached item } }; myiife.getFromArray = function (idToFind) { for (var i = 0, len =

JavaScript self-invoking function

强颜欢笑 提交于 2019-12-13 02:14:41
问题 I got this example from page 79 of a book called Object Oriented JavaScript by Stoyan Stefanov. Not really knowing what to do, the first time I ran this program (by hitting enter) it returned 'undefined'. After that, following the author's instructions, I called it a(); and got the alert 'Worky worky' My questions are a) Did I do the first step correctly? i.e. am I supposed to run a self-invoking program by merely hitting "enter/return"? b) if I was correct to just hit "enter/return" to run

why 'this' point to 'window' obj when using assignment operator in iife?

梦想与她 提交于 2019-12-12 09:55:40
问题 I was wondering why the example will return 'global' not 'obj2'? And what's different between '(obj2.say = obj1.say)()' and '(obj2.say)()'? Here is the code: var text = 'global'; var obj1 = { text: 'obj1', say: function () {console.log(this.text)}}; var obj2 = { text: 'obj2'}; (obj2.say = obj1.say)(); 回答1: The result of an assignment is the value that was assigned. Example: var foo, bar; foo = (bar = 42); console.log(foo); // 42 Hence when you do (obj2.say = obj1.say) , the result of the

How to create Javascript Object using IIFE

杀马特。学长 韩版系。学妹 提交于 2019-12-12 00:26:48
问题 I have a Student object like the following, function Student(){ this.studentName = ""; } Student.prototype.setStudentName=function(studentName){ this.studentName = studentName; } Student.prototype.getStudentName=function(){ return this.studentName; } It works when I do new Student(); . But If I create the same Object like the following it gives an error, (function(){ function Student(){ this.studentName = ""; } Student.prototype.setStudentName=function(studentName){ this.studentName =

Modules that are not IIFEs

二次信任 提交于 2019-12-11 13:07:41
问题 See Data dependency in module I have a few modules in my app which depend on data coming from the server. I implemented the modules as IIFEs, as the module pattern suggests, but in order to be able to reference them as the callback for the ajax request I am thinking of defining them as regular functions, and initialising them in the callback (see the answer in the other post for reference). Everywhere I looked, the module pattern consisted of IIFEs. What are the drawbacks(if any) of using

function inside of IIFE not recognized

自作多情 提交于 2019-12-11 09:03:55
问题 I have an IIFE that I am trying to make into a bookmarklet. I would like to have the modal the bookmarklet will pop up have some buttons that will call a function. However, when I have a structure like this: (function(){ var __myFn = function(str){ //also have tried function __myFn(){..} alert(str); } //some AJAX that builds HTML with the `result`s document.getElementById("resultDiv").innerHTML = "<span onclick='__myFn(" + result.someData+ ")'>test</span> }()); I get Uncaught ReferenceError:

Can someone fluent in Javascript explain to me whats going on here SIMPLY

会有一股神秘感。 提交于 2019-12-11 07:19:39
问题 Im taking a course on udemy and I came across this code that changes the background of a window. The thing is the function randColor loses me. Id like to know exactly whats going on. I know a function called randColor is declared, then the function itself RETURNS a function + # but I am trying to understand the logic of how it all happens. There is a HASH symbol that is added and I believe its also an IIFE correct? I very much appreciate the help! document.querySelector("button")

What is the functional difference between these two different Module pattern syntaxes

不羁的心 提交于 2019-12-11 05:53:37
问题 I see this syntax everywhere: var mod = (function(){ var pvtvar; var pvtfunc = function(){}; //return an object literal return { pubvar : 'whatever', pubfunc : function(){} }; }()); I recently came across this syntax: //first create empty object var mod = {}; (function(mod){ var pvtvar; var pvtfunc = function(){}; //modify the mod object argument mod.pubvar = 'whatever'; mod.pubfunc = function(){}; }(mod)); //pass object to IIFE I know that they both work, and I think I understand completely