最近看到一篇文章写到灵活的 Javascript ,甚是感叹Javascript的强大。
<script language="javascript">
Number.prototype.add=function(x){
return this+x ;
};
Number.prototype.subtract=function(x)
{
return this-x ;
} ;
Number.prototype.getRangeArray=function()
{
var arr=[] ;
for(var i=0;i<=this;i++)
{
arr.push(i) ;
}
return arr ;
};
Number.prototype= Object.defineProperty(Number.prototype,
"double",
{
get:function(){return (this+this);}
}
);
Number.prototype= Object.defineProperty(Number.prototype,
"square",
{
get:function(){return (this*this);}
}
);
//5+2
alert(5..add(2));
alert(5['add'](2));
alert((5).add(2));
//5+2-3
alert(5..add(2).subtract(3));
alert(5['add'](2)['subtract'](3));
alert((5).add(2)['subtract'](3));
alert(5..add(2)['subtract'](3));
//array 0 -5
alert((5).getRangeArray());
alert(5..getRangeArray());
alert(5['getRangeArray']());
//2*
alert((5).double);
alert(5..double);
alert(5['double']);
//x*x
alert((5).square);
alert(5..square);
alert(5['square']);
// (5+5)*(5+5)
alert((5).double.square);
alert(5..double.square) ;
alert(5['double']['square']);
alert(5['double'].square);
</script>