看闭包的代码 无法理解return lz的理解语法太乱。不太明白要表达什么意思 至于那个演示是执行一个方法,那个方法返回一个对象,对象里有2个方法,并且规定了count 的作用域,当执行的时候各count 之间互不影响。而lz的理解大概是想用面向对象的new来取代。虽然是可行的,但闭包只是js里的一个特性,如果只是学习的话就只要了解就行了,什么情况下方便就用。 就想for和each if else和switch case 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上面写的Counter是一种类似于面向对象的类。下面的Counter实际上还是一个函数。只是包含了两个内函数,共享count 变量。两部分都用到了闭包。上面的return把两个函数进行封装,使其成为类Counter的成员函数。所以才可以使用如下语法var foo =Counter(4);//创建一个Counter对象foofoo.increment();//调用对象foo的increment方法foo.get();// 5下面的写法显然是无法执行的。 通过函数创建对象 具体什么语法啊? 我看其他的一个教程里怎么有new 关键字呢?var Person = function(n,a,s) { var name = n; var sex = s; var age = a; return { get_Name : function() { return name; } , set_Name : function(v) { name = v; } , get_Sex : function() { return sex; } , set_Sex : function(v) { sex = v; } , get_Age : function() { return age; } , set_Age : function(v) { age = v; } };} var p = new Person("小王", 19, "男");alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex()); var p = Person("小王", 19, "男");alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex());是一样的 function Counter(start){ var count = start; return{ increment:function(){ count++; }, get:function(){ return count; } } } var foo =Counter(4); foo.increment(); foo.get();// 5问下我第一个例子中foo对象中 是否含有字段count 按二楼所说 2方法互不影响是什么意思? 我只想说我晕! return{ increment:function(){ count++; }, get:function(){ return count; } }这一段代码表示返回一个对象。这个对象里面有两个方法一个是increment,另一个是get。这两个方法都是在函数Counter里面定义的所以它们都是闭包。如果楼主不明白,楼主应该要自觉的看一下JS里面对象的创建和闭包的定义。因为是闭包所以increment和get它们都保存了对存有Counter变量对象的引用,所以它们都可以访问count。 Counter 函数相当于一个工厂方法,这个工厂方法产生一个对象并作为结果返回。注意,其返回的结果是一个对象字面量,不需要new,可以直接用。需要new的是用函数定义的类,建议楼主参考下JS里面向对象编程的相关知识。 其实,Counter和下面的写法是相同的。function Counter(start){ var count = start; var result = // result是一个对象字面量 { increment : function () { count++; }, get : function () { return count; } }; return result; } var foo = Counter(4); foo.increment(); foo.get(); // 5要Counter成为一个类,可以用下面的实现;Counter.prototype = { increment : function () { this.count++; }, get : function () { return this.count; }}然后就可以 new 了: var foo = new Counter(4); foo.increment(); foo.get(); // 5 第二段代码少了一些东西,下面是对的:function Counter(start){ this.count = start; }Counter.prototype = { increment : function () { this.count++; }, get : function () { return this.count; }} var foo = new Counter(4); foo.increment(); foo.get(); // 5 按你的理解(类似后台的类)应该是这样 function Counter(start) { this._count = start; } Counter.prototype.increment = function () { this._count ++; } Counter.prototype.get = function () { return this._count ; }p.s : prototype 不要直接覆盖,因为constructor有时会用到 . javascript 编写代码,仿qq相册上传图片 JSDT插件無法安裝到eclipse上? js随机显示一段js代码 如何给网页视频播放器指定若干个文件并自动轮流播放 一个框架调用问题? 请问eval()函数有什么用? 一个时间计算的问题,大家帮个忙吧。 能不能在一个js文件中引用(包含)另外一个js文件? 请问:如何用javascirpt在<TEXTAREA>中插入元素(光标位置)? 如何在文本框中设置搜索直接跳转,不通过百度显示关键词 你们下载的官网给的jquery easyui里的datagrid的demo一开始就能加载数据吗? 求助:JS闭包问题,循环添加事件,只有最后一个添加成功
下面的Counter实际上还是一个函数。只是包含了两个内函数,共享count 变量。
两部分都用到了闭包。
上面的return把两个函数进行封装,使其成为类Counter的成员函数。
所以才可以使用如下语法
var foo =Counter(4);//创建一个Counter对象foo
foo.increment();//调用对象foo的increment方法
foo.get();// 5下面的写法显然是无法执行的。
var Person = function(n,a,s) {
var name = n;
var sex = s;
var age = a;
return {
get_Name : function() {
return name;
}
,
set_Name : function(v) {
name = v;
}
,
get_Sex : function() {
return sex;
}
,
set_Sex : function(v) {
sex = v;
}
,
get_Age : function() {
return age;
}
,
set_Age : function(v) {
age = v;
}
};
}
var p = new Person("小王", 19, "男");
alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex());
alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex());是一样的
var count = start;
return{
increment:function(){
count++;
},
get:function(){
return count;
}
}
}
var foo =Counter(4);
foo.increment();
foo.get();// 5
问下我第一个例子中foo对象中 是否含有字段count 按二楼所说 2方法互不影响是什么意思?
increment:function(){
count++;
},
get:function(){
return count;
}
}这一段代码表示返回一个对象。这个对象里面有两个方法一个是increment,另一个是get。这两个方法都是在函数Counter里面定义的所以它们都是闭包。如果楼主不明白,楼主应该要自觉的看一下JS里面对象的创建和闭包的定义。因为是闭包所以increment和get它们都保存了对存有Counter变量对象的引用,所以它们都可以访问count。
注意,其返回的结果是一个对象字面量,不需要new,可以直接用。
需要new的是用函数定义的类,建议楼主参考下JS里面向对象编程的相关知识。
function Counter(start)
{
var count = start;
var result = // result是一个对象字面量
{
increment : function ()
{
count++;
},
get : function ()
{
return count;
}
};
return result;
}
var foo = Counter(4);
foo.increment();
foo.get(); // 5要Counter成为一个类,可以用下面的实现;
Counter.prototype =
{
increment : function ()
{
this.count++;
},
get : function ()
{
return this.count;
}
}然后就可以 new 了:
var foo = new Counter(4);
foo.increment();
foo.get(); // 5
{
this.count = start;
}Counter.prototype =
{
increment : function ()
{
this.count++;
},
get : function ()
{
return this.count;
}
}
var foo = new Counter(4);
foo.increment();
foo.get(); // 5
this._count = start;
}
Counter.prototype.increment = function () {
this._count ++;
}
Counter.prototype.get = function () {
return this._count ;
}p.s : prototype 不要直接覆盖,因为constructor有时会用到 .