Author: Good luck release date: March 4, 2008 category: Javascript
% Bulid %/Yahoo. js in the Yui framework contains a series of variable type detection methods, which are dividedYahoo. 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 and supports the splice method, // 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) {// safari has a bug, so you have to process the string if (OBJ & obj. constructor & obj. constructor. tostring (). indexof ('array')>-1) {return true;} else {return Yahoo. lang. isobject (OBJ) & obj. constructor = array ;}},
The array type is flawed, for example, the followingCode
Function myarray () {This. name = 'name';} var O2 = new myarray (); alert (Yahoo. util. isarray (O2); // bring up true // because obj. constructor. tostring () contains the words myarray, so truefunction OBJ () {This. name = 'name';} var o = new OBJ (); O. constructor = array; alert (Yahoo. util. isarray (o); // bring up true // because in Javascript, constructor is also an attribute // can be dynamically specified, so true is returned
Therefore, in later versions of Yui, Yahoo. Lang. isarray is modified to the current version.
Isarray: function (o) {If (o) {var L = Yahoo. lang; // if the object has the Length attribute and supports the splice method, // 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 tostring method of the JavaScript Object returns the description of the object. We often forget the existence of this property.
When viewing the jquery source code, we found that its isarray uses tostring for type determination:
597 tostring = object. Prototype. tostring;
...
616 isarray: function (OBJ ){
617 return tostring. Call (OBJ) === "[object array]";