YUI read code Diary of Yahoo.lang.is*_yui. Ext Related

Source: Internet
Author: User
Yahoo.lang = Yahoo.lang | | {
Isarray:function (o) {
if (o) {
var L = Yahoo.lang;
If the object has length this property, while supporting the splice method,
Then think of it 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 | | = = = =];
var L = Yahoo.lang;
Return (L.isobject (o) | | l.isstring (o) | |
L.isnumber (o) | | L.isboolean (o));
}
};...... Copy and paste Split line ...

It is reported that in the YUI 2.2.0 version before, YAHOO.lang.isArray is written in this way.

Isarray:function (obj) {
Safari has bugs and has to deal with strings
if (obj && obj.constructor &&
Obj.constructor.toString (). IndexOf (' Array ') >-1) {
return true;
} else {
return YAHOO.lang.isObject (obj) && obj.constructor = = Array;
}
And such a judgment array type is flawed, such as the following code

function MyArray () {
this.name = ' name ';
}
var O2 = new MyArray ();
Alert (YAHOO.util.isArray (O2)); Eject True
Returns True because the Obj.constructor.toString () contains the MyArray typeface

function Obj () {
this.name = ' name ';
}
var o = new OBJ ();
O.constructor = Array;
Alert (YAHOO.util.isArray (o)); Eject True
Because in JavaScript, constructor is also a property.
Can be specified dynamically, so return true therefore, in YUI's subsequent version, YAHOO.lang.isArray was modified to the present appearance

Isarray:function (o) {
if (o) {
var L = Yahoo.lang;
If the object has length this property, while supporting the splice method,
Then think of it as an array.
Return L.isnumber (o.length) && l.isfunction (O.splice);
}
return false;
The new implementation uses another idea: if the object has length this property and supports the splice method, it is considered an array. Of course, it still has bugs, and we can still create an object that has the length attribute and the Splice method. But I think the current implementation is more reasonable, because it is not possible, and then avoid the strange browser BUG.

YUI 2.3.0 After the introduction of the YAHOO.lang.isValue, in fact, is to determine whether the parameters are a meaningful value, as long as the parameters are not Null/undefined/nan, then return True. (Note that this differs from the general judgment that the 0/false/' (empty string) is a valid value), so YAHOO.lang.isValue is ideal for determining whether the value of a form field is a valid value.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.