admin管理员组文章数量:1279090
I asked a question about the undefined value in javascript a few days ago. (What is the best way to pare a value against 'undefined'?)
I conclude that it is a bad practice to do !== undefined
as undefined
can be set to 'another' value.
undefined='foo';
var b;
(b !== undefined) // true
I gave a quick look at the jquery code, I realized that in every part, the author use !== undefined
and not typeof var !== "undefined"
// Setting one attribute
if ( value !== undefined ) {
// Optionally, function values get executed if exec is true
exec = !pass && exec && jQuery.isFunction(value);
Is it a possible mistake? Even if I know that we should be crazy to reassign the value of undefined - for the most popular library I think it can cause some mistakes...
Who is in the right way?
I asked a question about the undefined value in javascript a few days ago. (What is the best way to pare a value against 'undefined'?)
I conclude that it is a bad practice to do !== undefined
as undefined
can be set to 'another' value.
undefined='foo';
var b;
(b !== undefined) // true
I gave a quick look at the jquery code, I realized that in every part, the author use !== undefined
and not typeof var !== "undefined"
// Setting one attribute
if ( value !== undefined ) {
// Optionally, function values get executed if exec is true
exec = !pass && exec && jQuery.isFunction(value);
Is it a possible mistake? Even if I know that we should be crazy to reassign the value of undefined - for the most popular library I think it can cause some mistakes...
Who is in the right way?
Share Improve this question edited Dec 20, 2024 at 9:36 dumbass 27.2k4 gold badges36 silver badges73 bronze badges asked Aug 21, 2011 at 20:52 JohnJohnGaJohnJohnGa 15.7k20 gold badges64 silver badges87 bronze badges 3-
"I conclude that it is a bad practice to do
!== undefined
asundefined
can be set..." No, the bad practice is to setundefined
to another value, and then to write code that acmodates that bad practice. – user113716 Commented Aug 21, 2011 at 21:03 - @patrick if you have a safer way to check if a variable is not defined, why not using it? – JohnJohnGa Commented Aug 21, 2011 at 21:14
-
Because I do not believe in writing cryptic code only to acmodate bad coding practices. The appropriate solution is to remove the offending code. Comparing to
undefined
is just fine, though I can see the benefit in creating a localundefined
so that you don't need to traverse to the global every time you want to access it. To create a localundefined
, I'd rather usevoid
, which always returnsundefined
.var undef = void 0;
But I'd only do that for performance purposes. – user113716 Commented Aug 21, 2011 at 21:22
2 Answers
Reset to default 12undefined
in the jQuery code is actually an undefined parameter of a function wrapping the whole code:
(function(window, undefined) {
// jQuery code here
// undefined is the undefined parameter
}(window)); // notice we don't pass a second argument here
That's perfectly safe, as the undefined
parameter is local to the function, and nobody except the code in this function can assign to it.
Using a more clear syntax:
var myFunc = function(window, undefined) {
// jQuery code here
// The undefined variable is the undefined parameter
// If the function has been called without a second argument,
// then the undefined parameter is undefined.
};
myFunc(window); // no second argument
If you reassign undefined
to something else and you expect to use a large library, then you get what you deserve as far as I'm concerned. It's perfectly OK to test if a variable is undefined
and jQuery needs to do that in many cases to tell which optional parameters are or aren't passed to various functions.
本文标签:
版权声明:本文标题:javascript - If it’s bad practice to compare an expression against the variable `undefined`, why is this commonplace in jQuery c 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264969a2368287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论