mootools
MooTools 1.3 beta 2 was recently released and you may see a few new methods implemented to String, Array, Number, and Function: from. The from method of each of those Types returns an object of that type. Simply put: you'll always receive back an object of that type based on what you give it.
MooTools 1.3 beta 2是最近发布的,您可能会看到一些针对String,Array,Number和Function实现的新方法: from 。 这些类型中的每个类型的from方法都将返回该类型的对象。 简而言之:您将始终根据所提供的对象来返回该类型的对象。
新方法代码 (The New Method Code)
Function.from = function(item){
return (typeOf(item) == 'function') ? item : function(){
return item;
};
};
Array.from = function(item){
if (item == null) return [];
return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : Array.prototype.slice.call(item) : [item];
};
Number.from = function(item){
var number = parseFloat(item);
return isFinite(number) ? number : null;
};
String.from = function(item){
return item + '';
};
The from method is added to String, Array, Number, and Function natives. Enough with the underlying code though -- examples are easier to understand.
from方法将添加到String,Array,Number和Function本机。 尽管底层代码足够了-示例更容易理解。
Function.from,Array.from,Number.from,String.from示例 (Function.from, Array.from, Number.from, String.from Examples)
Array.from('Item');
//returns ['Item'] (array type)
Function.from('Item, Whoa, Hey');
//returns function() { return 'Item, Whoa', Hey'; } (function type)
String.from(function() { alert('MooTools FTW!'); });
//returns function () { alert("MooTools FTW!"); } (string type)
Number.from('8765309');
//returns 8765309 (number type)
Each example above shows you what's returned by each method. Being able to generate a given object type from any argument using from can save you a lot of time -- especially when a given MooTools class or method requires an argument of a specific type. from is just another example of how MooTools can make your JavaScript life easier!
上面的每个示例都向您展示了每种方法返回的结果。 能够使用from从任何参数生成给定的对象类型可以节省大量时间-尤其是当给定的MooTools类或方法需要特定类型的参数时。 从是MooTools的如何使你JavaScript的生活更轻松只是另一个例子!
mootools