Javascript: a; vs. this.a;
今天想用underscore的_.isUndefined函数判断一个变量是不是未定义(undefined),结果令我非常的迷惑。
> _.isUndefined
function (obj) { return obj === void 0; }
> void 0;
undefined
> a;
ReferenceError: a is not defined
> a === void 0;
ReferenceError: a is not defined
> window.a === void 0;
true
> _.isUndefined(a)
ReferenceError: a is not defined
> _.isUndefined(window.a)
true
> window.a;
undefined
> this
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
> this.a;
undefined
> a;
ReferenceError: a is not
[End]
上面代码中蓝色部分是输入,黑色部分是输出。执行环境:Google Chrome v26.0, underscore版本1.4.4.
有图为证,如下:
我觉得这个应该是个很基础的问题,找到答案后再来更新本文。如果你刚好路过,而且知道这个问题,哇哦,亲,太好了,指点一下吧。
If you love him, teach him C++, for it's heaven;
If you hate him, teach him C++, for it's hell
If you hate him, teach him C++, for it's hell