• (nodebb)

    Wait, isn't the first one just a non-terminated recursive method eating up stack till it runs out? I'm curious, because I never attempted to overload in JavaScript, I didn't even know that a redefinition doesn't result in an error.

  • some guy (unregistered)

    One can have sort-of-overloading by type-testing on the duck-typed arguments - but it's not a great idea.

  • (nodebb) in reply to MaxiTB

    Wait, isn't the first one just a non-terminated recursive method eating up stack till it runs out?

    It is, but it never gets called because:

    I'm curious, because I never attempted to overload in JavaScript, I didn't even know that a redefinition doesn't result in an error.

    Indeed. That's ... disappointing, but not entirely unsurprising. It's a bit like all the fun you can have in Python if you carelessly assign a new value to self.frodo when self.frodo exists and is (in theory) a function. The function object disappears and whatever you assigned takes its place.

    $ python
    Python 3.8.10 (default, Sep 11 2024, 16:02:53) 
    [GCC 9.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Furble:
    ...    def frodo(bilbo):
    ...       print("hello")
    ... 
    >>> f = Furble()
    >>> f
    <__main__.Furble object at 0x7f3cf9311fd0>
    >>> f.frodo
    <bound method Furble.frodo of <__main__.Furble object at 0x7f3cf9311fd0>>
    >>> f.frodo = 7
    >>> f.frodo
    7
    

    Luckily:

    >>> del f.frodo
    >>> f
    <__main__.Furble object at 0x7f3cf9311fd0>
    >>> f.frodo
    <bound method Furble.frodo of <__main__.Furble object at 0x7f3cf9311fd0>>
    

    But also:

    ...    def frodo(bilbo):
    ...       print("hello")
    ...    def frodo(baggins,ring):
    ...       print("goodbye")
    ... 
    >>> f = Furble()
    >>> f.frodo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: frodo() missing 1 required positional argument: 'ring'
    >>> f.frodo("a",7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: frodo() takes 2 positional arguments but 3 were given
    >>> f.frodo("a")
    goodbye
    >>> del f
    >>> del Furble
    >>> class Furble:
    ...    def frodo(self,bilbo):
    ...        print("hello",bilbo)
    ...    def frodo(self,baggins,ring):
    ...        print("goodbye",baggins,ring)
    ... 
    >>> f = Furble()
    >>> f.frodo("a",7)
    goodbye a 7
    

    So Python does it, too.

  • (nodebb)

    Just as i said yesterday that we have a leading candidate for upper management, competition emerged the very next day.

  • (nodebb)

    The greasy pole has lotsa wannabe's scrabbling at the bottom. And more every day.

  • (nodebb) in reply to Mr. TA

    That's pretty much every day in WTFLand.

  • Scragar (unregistered)

    I doesn't default unpassed in arguments to null, it defaults them to undefined. A similar value, but still distinct.

    null !== undefined , typeof undefined === "undefined" , and typeof null === "object" are all true in JS.

  • Sauron (unregistered)

    That it worked at all was a delightful coincidence

    That the parser accepts it in the first place is heresy.

    Like, in nested scopes why not. But declaring 2 functions with the same identifier in the same scope? The language shouldn't allow that. JS is TRWTF.

  • (nodebb) in reply to Sauron

    JS is definitely very WTFy, but there isn't much choice - that's the language in all the browsers.

    TRWTF is using JS on the server, or using languages like PHP - there are much better choices in that arena.

  • (nodebb)

    it just defaults the remainders to null

    Actually it defaults them to undefined. But that's also falsey, so the logic is the same.

  • (nodebb)

    That senior dev might be thinking of TypeScript, which on the surface does have operator overloading... but only until you compile it down to JavaScript, at which point yeah, it's gone.

    Also, any number of tools would've caught that (ESLint, IDE's). Which makes one wonder what editor that senior dev was using.

Leave a comment on “An Overloaded Developer”

Log In or post as a guest

Replying to comment #:

« Return to Article