30 Day JavaScript Learning Challenge: Day 16

Day 16 and once again JavaScript doesn't fail to bring about a WTF moment.

Consider the following object:

var anObject = {left: 1, right: 2};

if we call each of the methods of this object it does what we expect:

> anObject.left
1
> anObject.right;
2

But here's the kicker. Let's say we decide to remove the left element, but we manage to fat-finger it:

> anObject
{ left: 1, right: 2 }
> delete anObject.lect
true

Really? When I delete something that's not there it returns true?

What happens when I actually delete the method:

> anObject
{ left: 1, right: 2 }
> delete anObject.left
true
> anObject
{ right: 2 }

Hm, failure looks remarkably similar to success (which I believe is the JavaScript Mantra).

How does Python handle this?

class foo():
    def foo():
        pass

> delattr(foo, 'bar')

> delattr(foo, 'squeegee')

AttributeError: class foo has no attribute 'squeegee'

Once again success looks different from failure in Python (the latter throws an AttributeError exception).

I guess the JavaScript rule is "if the delete returns true you can be assured it's gone", but it's another in a long line of things that JavaScript tends to handle strangely for me.


links

social