How to return object literal in JavaScript

Iain

I'm trying to wrap a JavaScript object literal in a self executing anonymous function. The first code example below works fine, but the second doesn't and I'm not really sure why?

Works:

(function(){
    return MyApp = {
        init: function() {
            console.log('MyApp init');
        }
    }
})();

Doesn't Work:

(function(){
    var MyApp = {
        init: function() {
            console.log('MyApp init');
        }
    }
    return MyApp;
})();

As I understand things, the SEAF should execute and immediately return. That's why the first example returns MyApp as an object I can interact with. I thought assigning MyApp to a variable inside the SEAF and then returning it would do the same thing but in:

Uncaught ReferenceError: MyApp is not defined 

Why?

Bergi

Since the result of your SEAF (better named IEFE) is not used anywhere, it doesn't really matter what the function returns. Now compare

(function(){
    MyApp = {…}
})();

with

(function(){
    var MyApp = {…}
})();

The difference is that in the second function your variable is preceded by a var keyword which makes it local to the IEFE, while in the first function it is an implicit global (which you should avoid). That way, the second snippet doesn't assign to anything in the global scope, and accessing MyApp later from outside will fail with the error.

Better return some value that you then assign to a globally declared variable:

var MyApp = (function(){
    return {…};
})();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return a default property of JS literal object?

From Java

How to use a variable for a key in a JavaScript object literal?

From Dev

How to conditionally add properties to a javascript object literal

From Dev

how to check a literal object is in an Javascript array

From Dev

how to check a literal object is in an Javascript array

From Dev

How to define function return type of a custom object literal

From Dev

How to use return value from another JS function in object literal

From Dev

How can I convert the string of a JavaScript object literal to a JavaScript object literal?

From Dev

object literal in JavaScript

From Dev

In Javascript is an array literal an object?

From Dev

JavaScript namespace object literal

From Dev

How to reference an object property when defining an object literal in Javascript?

From Dev

How to add object literal within object literal

From Dev

Javascript - how to bind 'this' inside an ajax call to the object literal

From Dev

How Can I Check a Object Literal's Name in JavaScript?

From Dev

Javascript - how to bind 'this' inside an ajax call to the object literal

From Dev

creating javascript object literal of arrays

From Dev

JavaScript object literal property scope

From Dev

Javascript literal object and singleton pattern

From Dev

Javascript: pass var to object literal

From Dev

Return JavaScript object literal, not JSON string, from ASP.NET MVC endpoint

From Java

Dealing with functions that return object literal in Typescript

From Dev

How can I return an anonymous object in Javascript?

From Dev

How to return an object of an element with javascript or jquery

From Dev

How to return object if it contains a letter in JavaScript

From Dev

Javascript object: How to return property using value?

From Dev

How to return the key from object value in javascript?

From Dev

How to write a Jackson pojo class for a json object like a javascript object literal

From Dev

How to write literal Javascript in Typescript

Related Related

  1. 1

    How to return a default property of JS literal object?

  2. 2

    How to use a variable for a key in a JavaScript object literal?

  3. 3

    How to conditionally add properties to a javascript object literal

  4. 4

    how to check a literal object is in an Javascript array

  5. 5

    how to check a literal object is in an Javascript array

  6. 6

    How to define function return type of a custom object literal

  7. 7

    How to use return value from another JS function in object literal

  8. 8

    How can I convert the string of a JavaScript object literal to a JavaScript object literal?

  9. 9

    object literal in JavaScript

  10. 10

    In Javascript is an array literal an object?

  11. 11

    JavaScript namespace object literal

  12. 12

    How to reference an object property when defining an object literal in Javascript?

  13. 13

    How to add object literal within object literal

  14. 14

    Javascript - how to bind 'this' inside an ajax call to the object literal

  15. 15

    How Can I Check a Object Literal's Name in JavaScript?

  16. 16

    Javascript - how to bind 'this' inside an ajax call to the object literal

  17. 17

    creating javascript object literal of arrays

  18. 18

    JavaScript object literal property scope

  19. 19

    Javascript literal object and singleton pattern

  20. 20

    Javascript: pass var to object literal

  21. 21

    Return JavaScript object literal, not JSON string, from ASP.NET MVC endpoint

  22. 22

    Dealing with functions that return object literal in Typescript

  23. 23

    How can I return an anonymous object in Javascript?

  24. 24

    How to return an object of an element with javascript or jquery

  25. 25

    How to return object if it contains a letter in JavaScript

  26. 26

    Javascript object: How to return property using value?

  27. 27

    How to return the key from object value in javascript?

  28. 28

    How to write a Jackson pojo class for a json object like a javascript object literal

  29. 29

    How to write literal Javascript in Typescript

HotTag

Archive