Y.Lang.isUndefined vs typeof undefined, which is better and why?

為{幸葍}努か 提交于 2020-01-14 22:55:11

问题


I was wondering why YUI libs use Y.Lang.isUndefined() even when JS has a built in feature to check type of a variable.


回答1:


The reason that method exists is for API consistency -- the YUI team felt it would be weird to leave it out. As LightStyle points out, isUndefined() doesn't do anything special, so you probably shouldn't use it unless you really like the aesthetics.

In fact, only a few of the YUI type checking methods do any actual useful work beyond what you could do natively. Ryan Grove has a detailed walkthrough of these methods (and why you should avoid most of them) in his talk, When Not To Use YUI (youtube.com)




回答2:


There is no a true reason in my opinion, it was only for development purposes(it is easier to call a function insted of writing typeof myVar === 'undefined').

L.isUndefined = function(o) {
    return typeof o === 'undefined';
};

From YUI.js source - row 1973



来源:https://stackoverflow.com/questions/16751609/y-lang-isundefined-vs-typeof-undefined-which-is-better-and-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!