Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Function Definitions

I was wondering why the Vector variable defined within this self executing javascript function doesn't require a var before it? Is this just some other type of syntax for creating a named function? Does this make it so we can't pass Vector as an argument to other functions?

(function() {
    Vector = function(x, y) {
        this.x = x;
        this.y = y;

        return this;
    };

   //...snip   
})()
like image 872
Pierreten Avatar asked Feb 27 '23 01:02

Pierreten


1 Answers

The code construct above makes Vector a global variable in the namespace, which might be OK since it is probably intended to be used as constructor.

I would not recommend adding to the global name space, actually take a look at requirejs its a very nice way to work with modular JS.

like image 186
Ernelli Avatar answered Mar 07 '23 17:03

Ernelli