What's the difference between these two functions in JavaScript?

user2204449

Type 1 :

var EXAMPLE = function() {};

Type 2:

var EXAMPLE = function($) {
    return {};
}(jQuery);

I'm JavaScript beginner and trying to understand the difference between these two piece of code. In functionality wise I didn't see the difference when I tried to add a property to the EXAMPLE object.

T.J. Crowder

There are a couple of major differences there:

  1. With the first one, the value of EXAMPLE ends up being a function object. With the second one, it ends up being a plain object (not a function), because in the second one, the function is being called and you're storing its return value (the {}) in EXAMPLE.

  2. The second one relies on your having a jQuery symbol defined (probably as a result of including jQuery on the page). The first doesn't.

So they're pretty different.

The first is a pretty generic way of creating a function (in this case, a function that doesn't do anything).

The second is a fairly standard way to use jQuery without relying on the $ global (because sometimes people use jQuery.noConflict() to release the $ symbol). The idea is that since jQuery is being passed into the function as the $ argument, within the function code can use $ even though there is no $ global. People routinely do this and return an object with functions on it, like this:

jQuery.noConflict(); // Release $
display("typeof $ = " + typeof $);   // "undefined"

var EXAMPLE = function($) {
  return {
    red: function(selector) {
      // Note we can use $ here, even though $ isn't
      // defined globally
      $(selector).css("color", "red");
    },
    green: function(selector) {
      $(selector).css("color", "green");
    }
  };
}(jQuery);

setTimeout(function() {
  EXAMPLE.red("#target");
}, 700);

setTimeout(function() {
  EXAMPLE.green("#target");
}, 1400);

function display(msg) {
  jQuery("<p>").html(String(msg)).appendTo(document.body);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="target">I'm the target element</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the difference between these two functions in Javascript?

From Dev

What exactly is the difference between these two javascript functions?

From Dev

What are all the difference between these two Javascript functions?

From Java

What's the difference between these two functions

From Dev

What's the difference between these two functions?

From Dev

What's the difference between these two statements in Javascript?

From Dev

What's the difference between these two statements in Javascript?

From Dev

What is the difference between these two ways of changing a functions prototype in JavaScript?

From Dev

Javascript : What is the difference between these two fat arrow functions?

From Dev

What's the difference between these two ways to declare functions?

From Dev

What's the difference between these two ways of writting a prototype function in JavaScript

From Dev

Please what's the difference between the two items in JavaScript?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference between these two recursive ocaml functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What is the difference between scope of these two functions

From Dev

Javascript - What is the difference between these constructor functions?

From Dev

What is the difference between anonymous and inline functions in JavaScript?

From Dev

What is the difference between these JavaScript anonymous functions?

From Dev

Javascript - What is the difference between these constructor functions?

From Dev

What is the difference between these two snippets of javascript code?

From Dev

What's the difference between these two async methods?

From Dev

What's the difference between these two function expressions?

From Dev

What's the difference between these two lines?

From Dev

What's the difference between these two new syntaxes?

Related Related

  1. 1

    What is the difference between these two functions in Javascript?

  2. 2

    What exactly is the difference between these two javascript functions?

  3. 3

    What are all the difference between these two Javascript functions?

  4. 4

    What's the difference between these two functions

  5. 5

    What's the difference between these two functions?

  6. 6

    What's the difference between these two statements in Javascript?

  7. 7

    What's the difference between these two statements in Javascript?

  8. 8

    What is the difference between these two ways of changing a functions prototype in JavaScript?

  9. 9

    Javascript : What is the difference between these two fat arrow functions?

  10. 10

    What's the difference between these two ways to declare functions?

  11. 11

    What's the difference between these two ways of writting a prototype function in JavaScript

  12. 12

    Please what's the difference between the two items in JavaScript?

  13. 13

    What is the difference and issues between these two clojure functions?

  14. 14

    What is the difference between these two recursive functions?

  15. 15

    What is the difference between these two recursive ocaml functions?

  16. 16

    What is the difference between these two ruby functions?

  17. 17

    What is the difference between these two recursive functions?

  18. 18

    What is the difference and issues between these two clojure functions?

  19. 19

    What is the difference between these two ruby functions?

  20. 20

    What is the difference between scope of these two functions

  21. 21

    Javascript - What is the difference between these constructor functions?

  22. 22

    What is the difference between anonymous and inline functions in JavaScript?

  23. 23

    What is the difference between these JavaScript anonymous functions?

  24. 24

    Javascript - What is the difference between these constructor functions?

  25. 25

    What is the difference between these two snippets of javascript code?

  26. 26

    What's the difference between these two async methods?

  27. 27

    What's the difference between these two function expressions?

  28. 28

    What's the difference between these two lines?

  29. 29

    What's the difference between these two new syntaxes?

HotTag

Archive