r/programming • • Nov 17 '14

Rob Eisenberg: "Leaving Angular"

http://eisenbergeffect.bluespire.com/leaving-angular/#
185 Upvotes

104 comments sorted by

View all comments

Show parent comments

1

u/flukus Nov 18 '14

Everything is a function in javascript, this is simple it's way of mimicking static in java etc.

The only time I've ever seen this be a problem is from incompetent devs, who created much bigger problems to deal with.

1

u/bstamour Nov 18 '14

Fine, but you're still incorrect about how static behaves in those languages. It does not create global variables...

1

u/flukus Nov 18 '14

Yes it does. They might be namespaced but they are still global variables.

1

u/bstamour Nov 18 '14
void foo() {
    static int x = 10;
    printf("%d", x);
    ++x;
}

Now, how do I access x from outside foo? You can't, because it isn't global.

1

u/flukus Nov 18 '14

That's only one use of static, this creates a global variable (or my c++ is worse than I realized):

class AClass{ public: static int static_field = 0; };

1

u/bstamour Nov 18 '14

It is one use of static, but it is exactly the use I've been talking about. Unlike in Javascript, you cannot magically create a global variable from inside of a function. Static doesn't do that.

As for your example, yes static_field is kind of global, since it can be accessed anywhere that can see AClass's class definition, but that's a moot point when discussing javascript's ability to inject a variable into a global namespace. The equivalent to your C++ code would be to simply declare a global variable in Javascript.

Context is everything. You cannot simulate the concept of implicit globals using the static keyword.

1

u/flukus Nov 19 '14

In what language does the static keyword work like that? It definitely doesn't work that way in C, C++, Java, C#

I assumed you were talking about how static worked in all of them, java and c++ don't have inline static.

Context is everything. You cannot simulate the concept of implicit globals using the static keyword.

The context is that it's a functional language with a very different type system (for better and worse). Implicit globals are how you define objects/classes as well.