StoneIT_ZL的博客通过本文主要向大家介绍了面向对象,闭包,函数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
如下这段闭包相关的代码:
var extent = function(){
var value = 0;
return {
call: function(){
value++;
console.log( value );
}
}
};
var extent = extent();
extent.call(); // 输出: 1
extent.call(); // 输出: 2
extent.call(); // 输出: 3
换成面向对象的写法如下:
var extent = {
value: 0,
call: function(){
this.value++;
console.log(this.value);
}
}
extent.call(); // 输出: 1
extent.call(); // 输出: 2
extent.call(); // 输出: 3
或者
var Extent = function(){
this.value = 0;
}
Extent.prototype.call = function(){
this.value++;
console.log(this.value)
}
extent.call(); // 输出: 1
extent.call(); // 输出: 2
extent.call(); // 输出: 3