What is the benefit of angular.isdefined?

后端 未结 4 1773
走了就别回头了
走了就别回头了 2021-02-02 05:15

What is the benefit of angular.isdefined over and above foo === undefined?

I can\'t immediately think of a benefit.

4条回答
  •  不要未来只要你来
    2021-02-02 05:41

    Accessing a truly undefined variable in any way in Javascript, except typeof throws an error. You can only use Angular.isDefined with properties. E.g, this would work fine:

    angular.isDefined(window.obj);
    

    Because obj is an undefined propery of window.

    Examples of the expected behavior:

    var foo;
    var bar = 42;
    
    typeof foo !== 'undefined'; // false
    typeof bar !== 'undefined'; // true
    typeof baz !== 'undefined'; // false
    
    angular.isDefined(foo); // false
    angular.isDefined(bar); // true
    angular.isDefined(baz); // ReferenceError
    

提交回复
热议问题