YAHOO.lang的jQuery版本(isUndefined,isNull,isString等)

时间:2009-05-19 10:55:19

标签: javascript jquery yui

有没有人知道jQuery插件有“助手”或扩展名,如YAHOO.lang namespace中的那些?

我想到的功能如:

isNull
isDefined
isString
isFunction

我也很欣赏字符串和数组的相同类型,例如Contains,StartsWith(我知道这些很容易编写,我只是在寻找一个包含它们的插件)。

它不在YAHOO.lang名称空间中,而是形成相关的扩展名 - 确定一个radiobox的值(来自选中的一个),一个友好名称中的表单元素类型。

特别是一个具有流畅API而非基于选择器的插件,例如

$("input[@type=radio][@checked]")

我再次意识到它们很容易实现,但我不想重新发明轮子。

3 个答案:

答案 0 :(得分:2)

jQuery 1.3.2内置了isFunctionisArray(请参阅下面的代码段)。 isString的代码是staightforward(typeof obj === "string"),isNull(obj === null)和isDefined(obj !== undefined) - 所以我只是编写内联而不是使用函数。

// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
    return toString.call(obj) === "[object Function]";
},

isArray: function( obj ) {
    return toString.call(obj) === "[object Array]";
},

答案 1 :(得分:1)

Underscore.js或_.js,如果您愿意,则是包含这些功能的库。

答案 2 :(得分:0)

我决定自己写两个新的插件。这是两个项目:

Form extensions

示例:

// elementExists is also added
if ($("#someid").elementExists())
  alert("found it");

// Select box related
$("#mydropdown").isDropDownList();

// Can be any of the items from a list of radio boxes - it will use the name
$("#randomradioboxitem").isRadioBox("myvalue");
$("#radioboxitem").isSelected("myvalue");

General extensions

这些是以Prototype / Mochikit函数为模型的,例如isNull。

示例:

$.isNumber(42);

var x;
$.isUndefined(x);
$.isNullOrUndefined(x);
$.isString(false);

$.emptyString("the quick brown fox");
$.startsWith("the quick brown fox","the");
$.formatString("Here is the {0} and {2}","first","second");

两者都有超过50个单元测试作为下载的一部分。希望它们对找到这个页面的人有用。