iife

understanding $ vs. jQuery in iife instead of $

☆樱花仙子☆ 提交于 2019-11-27 05:35:24
I am trying to understand if there is any difference between: (function($){ ... })(jQuery); vs. (function($){ ... })($); Note the jQuery was replaced with a $. Is this ok? Is it not used anywhere because it can't work? It works but maybe it is non-standard? Can someone please explain this if it is an error or if it is ok? Thanks Other JavaScript frameworks may also use $ as a shortcut. To guarantee that $ is jQuery inside your function, pass jQuery and not $ at the end. This type of defining a function or "code area" is to take sure that $ really is jQuery when mixing frameworks. Tats_innit

JQuery best practice, using $(document).ready inside an IIFE?

不打扰是莪最后的温柔 提交于 2019-11-27 05:13:05
问题 I am looking at a piece of code: (function($) { // other code here $(document).ready(function() { // other code here }); })(jQuery); I though the IIFE does the functions of $(document).ready, is this code correct? or can I just remove the $(document).ready and place the code directly inside the IIFE. 回答1: No, IIFE doesn't execute the code in document ready. 1. Just in IIFE: (function($) { console.log('logs immediately'); })(jQuery); This code runs immediately logs "logs immediately" without

Why use NOT operator on anonymous function call? (a la Knockout 2.1.0) [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-27 02:10:33
问题 Possible Duplicate: What does the exclamation mark do before the function? If you look at the source code for KnockoutJS 2.1.0 you will see a code structure like this start on line 7: !function(factory) { ... }(factoryDefinition); The not operator causes this expression to evaluate to true rather than undefined , but why bother? 回答1: This is a concise way to form an immediately executed function expression. Traditionally, people have used these two forms (function(){ }()); // Recommended by

Javascript self executing function “is not a function”

我的梦境 提交于 2019-11-27 00:37:33
问题 I have: var Init = (function() { my js goes here })(); And my js executes correctly when the page is loaded. I also have: $('form :checkbox').change(function() { Init(); }); But firebug says Init is not a function. 回答1: It isn't a function. (function() { ... })() evaluates the anonymous function right then . And the result of the evaluation apparently does not return a function-object in this case :-) Consider: f = (function() { return "not a function :(" })() alert(f()) and f = (function() {

IIFE context issues

限于喜欢 提交于 2019-11-26 23:15:59
问题 In the following construct: (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!"); } this.show = function(){ alert("This is show function!"); } })(); Why does this refer to window object? Should everything inside IIFE be isolated from global scope? Are x and y functions also properties of window global object? Also, even if I use put var h = ... at the beginning: var h = (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!");

JavaScript Bang “!” Functions vs Leading Semi-Colon “;” IIFEs

纵饮孤独 提交于 2019-11-26 21:42:25
问题 Airbnd suggests I do this: !function() { // ... }(); Because: This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated. The bang allows me to work around the language's grammar rules: // Evaluated in Chromium 34 console. function(){}(); // => SyntaxError: Unexpected token ( !function(){}(); // => true And when concatenating other modules the bang seems to do the trick: !function(){}();function(){}(); // =>

Immediately-Invoked Function Expression (IIFE) In JavaScript - Passing jQuery

谁都会走 提交于 2019-11-26 17:18:23
问题 I've got the following code which I know is an IIFE. However, I've never been able to grasp what the (jQuery) and ($) is. I know it's got something to do with passing a reference of jQuery into the IIFE, but could someone explain me clearly the purpose of them? Thank you for you help and time :-) (function ($) { //code })(jQuery); 回答1: $ is an argument to a function. jQuery is what is being passed as that argument when the function is invoked. Think of it like this: function init($) { // code

What are the different ways of writing an IIFE? What are their use cases?

左心房为你撑大大i 提交于 2019-11-26 16:37:42
问题 I have started reading this book. Chapter 2 says that there are different ways to write an IIFE: !function (){}() ~function (){}() +function (){}() -function (){}() new function (){} 1,function (){}() 1&&function (){}() var i=function (){}() The author says: Each manifestation has its own unique qualities and advantages—some with fewer bytes, some safer for concatenation, each valid and each executable. I'm a newbie to JS. I know what an IIFE is, but what do these IIFE forms do exactly? 回答1:

Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]

烂漫一生 提交于 2019-11-26 15:37:38
Possible Duplicate: What is the (function() { } )() construct in JavaScript? I came across this bit of JavaScript code, but I have no idea what to make out of it. Why do I get "1" when I run this code? What is this strange little appendix of (1) and why is the function wrapped in parentheses? (function(x){ delete x; return x; })(1); Howard There are a few things going on here. First is the immediately invoked function expression (IIFE) pattern: (function() { // Some code })(); This provides a way to execute some JavaScript code in its own scope. It's usually used so that any variables created

!function(){ }() vs (function(){ })()

怎甘沉沦 提交于 2019-11-26 15:24:00
While reviewing some of the code written in the Twitter Bootstrap Javascript, it looks like they're calling immediately invoked anonymous functions like this: !function( $ ) { ... }(window.jQuery || window.ender); Where I've traditionally seen this same thing accomplished this way: (function($) { ... })(window.jQuery || window.ender); The first way seems a bit hacky, and I'm not sure if there is any benefit or reason for doing it this way rather than the second way? Note that I understand how it works, I'm looking to understand why they chose that way to do it. One less character when minified