1:打乱一个数组的顺序
var list = [1,2,3,4,5,6,7,8,9];
list = list.sort(function() Math.random() > 0.5 ? 1 : -1);
Print(list); //可以得到类似下面的结果: 4,3,1,2,9,5,6,7,8
2:从数组中移除一个对象
function RemoveArrayElement( array, element ) !!let (pos=array.lastIndexOf(element)) pos != -1 && array.splice(pos, 1);
3:把浮点数转换为数值
(123.345456).toFixed(); // is: 123
typeof (1.5).toFixed(); // is: string
4:返回一个对象属性的个数
({ foo:55, bar:99 }).__count__ // is: 2
5:检查一个对象是否为空
javascript 1.5下:
function isNotEmpty(obj) {
for ( var tmp in obj )
return true
}
javascript 1.8下:
function isNotEmpty(obj) obj.__count__;
6:单件模式
function MySingletonClass() {
if ( arguments.callee._singletonInstance )
return arguments.callee._singletonInstance;
arguments.callee._singletonInstance = this;
this.Foo = function() {
// ...
}
}
var a = new MySingletonClass()
var b = MySingletonClass()
Print( a === b ); // prints: true
转自:http://buluzhai.blogspot.hk/2009/05/javascript.html

浙公网安备 33010602011771号