Hacker News new | ask | show | jobs
by jpolitz 4921 days ago
Indeed --- Python goes to great lengths to work this way, automatically binding the self argument on method extraction.

    o.f()
Can be decomposed in Python:

    func = o.f
    func()
JavaScript fails this test, and it's a shame.
2 comments

Ho, you beat me to it with the Python reference (see my other comment in this thread) :)

I don't know if it's really that bad that JS returns the unbound function when doing `obj.someFunc`, as it is consistent with any other property access: it returns the same thing that was assigned to that key in the object, or at some point in its prototype chain.

The really broken behavior, at least for me, is that when doing `var f = obj.someFunc(); f()`, the "this" gets bound to "window" in the f() call, instead of being null, or better yet raising an error whenever referenced, like an undefined variable.

It's actually a Javascript strength; one that makes it more dynamic.

    NodeList.prototype.map = Array.prototype.map
Just one line and now you can do stuff like:

    document.querySelectorAll("div").map(function(ele){
      return ele.id
    });