tl; dr; : angular.isDefined(obj) not a complete replacement for typeof .
Did I miss something?
I do not think so. typeof is a special operator that does not throw an error if obj does not exist at all. However, passing a variable to a function will result in an attempt to read the value of the variable and, therefore, throw an error if it does not exist. There is no way to prevent this.
AFAIK typeof is the only statement that does not throw if it encounters a reference error. On the other hand, if you need to check whether a variable exists or not, your code is probably poorly designed (unless you need to check for the presence of "functions" (for example, third-party modules)).
Examples of 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
source share