In summary: Although angular.isDefined(obj)
is useful, it cannot completely replace the functionality of typeof
.
Is there something I am overlooking?
I don't believe so. The typeof
operator behaves differently than passing a variable to a function. While typeof
won't throw an error if the variable doesn't exist, passing it to a function will. It's simply the nature of how JavaScript works.
As far as I know, typeof
is unique in handling reference errors without throwing. However, if you find yourself needing to constantly check if a variable exists, it may be a sign of poor coding practice (unless it is necessary for certain functionalities like checking for third-party modules).
Here are some examples to illustrate the expected behavior:
var foo;
var bar = 42;
typeof foo !== 'undefined'; // false
typeof bar !== 'undefined'; // true
typeof baz !== 'undefined'; // false
isDefined(foo); // false
isDefined(bar); // true
isDefined(baz); // ReferenceError