Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a meaning for this piece of code?

Tags:

javascript

It's late in the evening here so I may not be functioning very well, but I found this piece of code and I can't seem to figure out why it's used like this (NOTE: I understand what it does, what I don't understand is the meaning behind it).

(function() {

    var narcissus = {
        options: {
            version: 185,
        },
        hostGlobal: this
    };
    Narcissus = narcissus;
})();

Self-executing anonymous functions are used to avoid pollution of the global namespace, but this code doesn't need to introduce other variables than Narcissus so it could have very easily be rewritten as Narcissus = {...};. Some possible reasons I can think of are future-proofing of the code or implementation flaw. Is there something I fail to see?

like image 664
Gabi Purcaru Avatar asked Jul 30 '11 21:07

Gabi Purcaru


People also ask

What is the meaning of this line of code?

A line of code (LOC) is any line of text in a code that is not a comment or blank line, and also header lines, in any case of the number of statements or fragments of statements on the line. LOC clearly consists of all lines containing the declaration of any variable, and executable and non-executable statements.

What is a piece of code?

In this article, we define a piece of code by using the code Element. It is used to define the piece of computer code. During the creation of web pages, sometimes there is a need to display computer programming code.

What are the 4 types of code?

While the names of the coding paradigms sometimes vary, most experts agree on four primary types of code: imperative, functional, logical, and object-oriented.


1 Answers

From a maintainability standpoint it allows the author to later add code in a Closure scope that doesn't get leaked between the creation of narcissus and the assignment of Narcissus. Though in this case there isn't any code there so I don't see any gains other than the this stuff mentioned above.

like image 162
Anthony Sottile Avatar answered Nov 14 '22 21:11

Anthony Sottile