标签:indicator abr 返回值 tar com turn 效率 sed lap
一、在javaScript中闭包的五种表现形式如下:
1 /** 2 * Created by admin on 2016/12/26. 3 *//* 4 //向函数对象添加属性 5 function Circle(r){ 6 this.r=r; 7 } 8 Circle.prototype.PI=3.1415926; 9 Circle.prototype.area=function(){ 10 return this.PI*this.r*this.r; 11 }; 12 var c=new Circle(); 13 console.log("are:"+ c.area(1.0)); 14 15 //新建一个Object对象,并向其中添加属性,然后返回这个对象 16 var Circle=function(){ 17 var obj=new Object(); 18 obj.PI=3.1415926; 19 obj.area=function(r){ 20 return this.PI* r*r; 21 } 22 return obj; 23 } 24 var c=new Circle(); 25 console.log("are:"+ c.area(1.0)); 26 27 //新建一个对象,并向其中添加属性 28 var Circle=new Object(); 29 Circle.PI=3.1415926; 30 Circle.area=function(r){ 31 return this.PI*r*r; 32 } 33 console.log("are:"+ Circle.area(1.0)); 34 35 //直接构造一个对象,申明属性和方法 36 var Circle={ 37 ‘PI‘:3.1415926, 38 ‘area‘:function(r){ 39 return this.PI*r*r; 40 } 41 } 42 console.log(‘area:‘+Circle.area(1.0)); 43 //利用JavaScript内置的Function对象构造一个对象Circle,注意它所构造的变量为全局变量,并且每次调用Circle对象都会调用Function,在循环体内少用。 44 var Circle=new Function("this.PI=3.1415926;this.area=function(r){return this.PI*r*r;}"); 45 console.log(‘area:‘+(new Circle).area(1.0));
二、闭包的用途
二、Javascript闭包的用途
标签:indicator abr 返回值 tar com turn 效率 sed lap
原文地址:http://www.cnblogs.com/hot-destiny/p/6221773.html