首页 > 编程语言 > 详细

灵活多变的Javascript

时间:2015-02-11 10:54:17      阅读:232      评论:0      收藏:0      [点我收藏+]

最近看到一篇文章写到灵活的 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>


灵活多变的Javascript

原文:http://blog.csdn.net/yue7603835/article/details/43730813

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!