// 闭包函数 实现方法1
function test(r){
    this.r=r
}

test.pi=3.14

test.prototype.area=function(){
    return test.pi * this.r * this.r
}

// 闭包函数 实现方法2
var test2=function(){
    var obj=new Object();
    obj.pi=3.14;
    obj.area=function(r){
        return this.pi*r*r;
    }
    return obj;
}
var i=new test2;
console.log(i.area(8))

// 闭包函数实现方法3

var test3=new Object();
test3.pi=3.14;
test3.area=function(r){
    return this.pi*r*r;
}
console.log(test3.area(8))

//闭包函数实现方法4
var test4={
    'pi':3.14,
    'area':function(r){
        return this.pi*r*r;
    }
};

console.log(test4.area(8))

//闭包函数实现方法5
var test5=new Function('this.pi=3.14;this.area=function(r){return this.pi*r*r}');
var i=new test5();
console.log(i.area(9))

 

相关文章:

  • 2022-02-01
  • 2021-12-02
  • 2021-11-18
  • 2021-08-24
  • 2021-11-15
  • 2021-10-04
  • 2022-12-23
猜你喜欢
  • 2021-06-09
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2021-10-03
  • 2021-05-19
相关资源
相似解决方案