Ask HN: Why I hate JavaScript

1 points by onecooldev24 ↗ HN
Try this out in node

function createObject() { this.x = 3; return this; }

obj = createObj();

if (obj.misspeld_props == 3) {

// never executes, nor cause a crash

}

I hope I made my point clear. Creating large code base with such properties is a pain. This does not happen in python, or any other language. What are your thoughts.

2 comments

[ 2.8 ms ] story [ 12.2 ms ] thread
I am not sure the point you are trying to make is fully clear.

Your problem does not have anything to do with the assignment or createObject (just for the sake, the code as is isnt calling the function), but that accessing inexistent variables only returns undefined but no actual error.

Is that correct?

Why would it cause a crash?

In function createObject(), the `this` reference is bound to the local scope of the node file. You assign a global `obj` handle to that reference when it is returned from the invocation of `createObject`. You then compare (not write!) 3 with a nonexistent property (undefined) on that reference.

Why would that cause a crash?

Git gud, as the kids say these days.

Maybe do some reading:

http://stackoverflow.com/questions/27031663/default-binding-...

EDIT:

Node and Javascript have all kinds of other problems, but if you take a second to learn about how things like `this` are handled in the language, you won't be nearly as surprised later.