Function.prototype.memorized = function(key) {
this._values = this._values || {};
if (this._values[key] !== undefined) {
return this._values[key]
}
else {
//"this" is parent function object
this._values[key] = this.apply(this, arguments); /* the "this" passed as context object is optional? */
return this._values[key];
}
};
function isPrime(num) {
alert(this);
var prime = num != 1;
for (var i = 2; i < num; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
var a = isPrime.memorized(5);
alert(a);
var b = isPrime.memorized(5);
alert(b);
Thursday, October 15, 2009
memoried function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment