In YUI Framework's % BULID % yahooyahoo. js, it contains a series of variable type detection methods, which are divided into YAHOO. lang. is *. Most of these functions are encapsulated by the typeof operator. I am very interested in isArray and isValue functions. YAHOO. lang = YAHOO. lang | {
IsArray: function (o ){
If (o ){
Var l = YAHOO. lang;
// If the object has the length attribute, the splice method is also supported,
// It is considered as an array.
Return l. isNumber (o. length) & l. isFunction (o. splice );
}
Return false;
},
IsBoolean: function (o ){
Return typeof o = 'boolean ';
},
IsFunction: function (o ){
Return typeof o = 'function ';
},
IsNull: function (o ){
Return o = null;
},
IsNumber: function (o ){
Return typeof o = 'number' & isFinite (o );
},
IsObject: function (o ){
Return (o & (typeof o = 'object' |
YAHOO. lang. isFunction (o) | false;
},
IsString: function (o ){
Return typeof o = 'string ';
},
IsUndefined: function (o ){
Return typeof o = 'undefined ';
},
//...
IsValue: function (o ){
// Infinity fails
// Return (o | o = false | o = 0 | o = '');
Var l = YAHOO. lang;
Return (l. isObject (o) | l. isString (o) |
L. isNumber (o) | l. isBoolean (o ));
}
};...... Copy and paste the split line ......
It is reported that YAHOO. lang. isArray was written like this before YUI 2.2.0.
IsArray: function (obj ){
// If safari has a bug, you have to handle the string
If (obj & obj. constructor &&
Obj. constructor. toString (). indexOf ('array')>-1 ){
Return true;
} Else {
Return YAHOO. lang. isObject (obj) & obj. constructor = Array;
}
}, And such judgment of the array type is flawed, such as the following code
Function myArray (){
This. name = 'name ';
}
Var o2 = new myArray ();
Alert (YAHOO. util. isArray (o2); // true is displayed.
// Returns true because obj. constructor. toString () contains the words myArray.
Function Obj (){
This. name = 'name ';
}
Var o = new Obj ();
O. constructor = Array;
Alert (YAHOO. util. isArray (o); // true is displayed.
// In JavaScript, constructor is also an attribute
// Can be dynamically specified, so true is returned. Therefore, in subsequent versions of YUI, YAHOO. lang. isArray is changed to the current format.
IsArray: function (o ){
If (o ){
Var l = YAHOO. lang;
// If the object has the length attribute, the splice method is also supported,
// It is considered as an array.
Return l. isNumber (o. length) & l. isFunction (o. splice );
}
Return false;
}, The new implementation uses another idea: if the object has the length attribute and supports the splice method, it is considered as an array. Of course, it still has a vulnerability. We can still create an object that has the length attribute and the splice method. However, I think the current implementation is more reasonable, because it is unlikely, and the second is to avoid the strange browser BUG.
Looking at the YAHOO. lang. isValue introduced after YUI 2.3.0, it is actually to judge whether the parameter is a meaningful value. If the parameter is not null/undefined/NaN, true is returned. (Note that this is different from the general true/false/''(Null String). These values are valid values. lang. isValue is ideal for determining whether the value of a form field is a valid value.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.