不一样的Javascript(11)——函数与对象

1. 每个函数都是对象,因此函数上可以定义自己的属性。例如:

test.counter = 0;
function test() {
    console.log(test.counter);
    
    test.counter++;
}

test(); // 0
test(); // 1
test(); // 2

这样counter的值在多次调用函数test的时候能够保存下来,又避免了定义不必要的全局变量。

函数还可以把自己当作成一个数组对象,从而往自己身上存取多个数据。例如:

function fibonacci(n) {
    if(fibonacci[n - 2] === undefined) {
        fibonacci[n - 2] = fibonacci(n - 2);
    }
    
    if(fibonacci[n - 1] === undefined) {
        fibonacci[n - 1] = fibonacci(n - 1);
    }
    
    return fibonacci[n - 1] + fibonacci[n - 2];
}

fibonacci[0] = 0;
fibonacci[1] = 1;

console.log(fibonacci(10)); // 55
console.log(fibonacci(50)); // 12586269025

如果直接递归求fibonacci数列,由于有很多重复的计算将导致计算效率极低。为了避免重复计算,一个办法是把已经计算得到的中间结果缓存起来。在上述代码中,中间结果缓存在函数自身的数组中。

2. 如果往构造函数上添加属性,还能实现类似于C++/Java上的静态变量和静态函数。例如如下例子:

function Account(name) {  
    this.name = name;  
    this.id = Account.nextId++;  
}  
  
Account.nextId = 100;  
  
Account.prototype.toString = function() {  
    return "Name is: " + this.name + "; id is: " + this.id + ".";  
}  
  
var accountA = new Account("Harry");  
var accountB = new Account("Peter");  
  
console.log(accountA.toString()); // Name is: Harry; id is: 100. 
console.log(accountB.toString()); // Name is: Peter; id is: 101.

在上述代码中,我们通过构造函数Account创建对象。我们希望每个对象的id属性都不一样。这个id属性通过Account.nextId生成。此时,Account.nextId就相当于是类型Account的一个静态变量。

在看一个往构造函数添加变量和函数的例子:

function Complex(real, imaginary) {
    this.r = real; 
    this.i = imaginary; 
} 

Complex.prototype.add = function(that) {
    return new Complex(this.r + that.r, this.i + that.i);
};

Complex.prototype.toString = function() {
    return "{" + this.r + ", " + this.i + "}";
};

Complex.parse = function(s) {
    try {
        var m = Complex._format.exec(s); 
        return new Complex(parseFloat(m[1]), parseFloat(m[2]));
    } 
    catch (x) { 
        throw new TypeError("Can't parse \"" + s + "\" as a complex number.");
    }
};
Complex._format = /^\{([^,]+),([^}]+)\}$/;

var complex1 = Complex.parse("{1, 2}");
var complex2 = Complex.parse("{5, 3}");
var result = complex1.add(complex2);
console.log(result.toString()); // {6, 5}
在上面的代码中,Complex._format相当于类型Complex的静态变量,而Complex.parse相当于类型Complex的静态函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值