mootools_新的MooTools方法:.from()

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的生活更轻松只是另一个例子!

翻译自: https://davidwalsh.name/mootools-from

mootools

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值