Home > Web Front-end > JS Tutorial > body text

YUI code reading diary related to YAHOO.lang.is*_YUI.Ext

WBOY
Release: 2016-05-16 19:05:52
Original
932 people have browsed it

YAHOO.lang = YAHOO.lang || {
isArray: function(o) {
if (o) {
var l = YAHOO.lang;
// If the object has the attribute length , and supports the splice method,
                      // Then it is considered an array.
         return l.isNumber(o.length) && l.isFunction(o.splice);
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 dividing line ...

It is reported that before YUI version 2.2.0, YAHOO.lang.isArray was written like this.

isArray: function(obj) {
// Safari has a bug, so we have 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;
} }
}, Such judgment of the array type is flawed, such as the following code

function myArray() {
this.name = 'name';
}
var o2 = new myArray( ; () {
this.name = 'name';
}
var o = new Obj();
o.constructor = Array;
alert(YAHOO.util.isArray(o )); // Pop up true
// Because in JavaScript, constructor is also a property
// It can be dynamically specified, so it returns true. Therefore, in subsequent versions of YUI, YAHOO.lang.isArray was modified to the current This looks like

isArray: function(o) {
if (o) {
var l = YAHOO.lang;
// If the object has the attribute length, it also supports splice Method,
// Then consider 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 The length attribute also supports the splice method, so it is considered an array. Of course, it still has loopholes, we can still create an object with a length property and a splice method. But I think the current implementation is more reasonable, because firstly it is unlikely, and secondly it avoids weird browser BUGs.

Looking at YAHOO.lang.isValue introduced after YUI 2.3.0, it actually determines whether the parameter is a meaningful value. As long as the parameter is not null/undefined/NaN, then it will return true. (Note that the difference between this and the general judgment of true and false is that 0/false/'' (empty string) are considered valid values), so YAHOO.lang.isValue is very suitable for judging whether the value of a form field is valid. value.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template