- 浏览: 90739 次
- 性别:
- 来自: 深圳南山
最新评论
-
ccc17638:
发现一个js replace正则相关的诡异问题 -
libmw:
我觉得我大学四年很苦,但是发现有人跟我一样苦,只是我干得太杂了 ...
我的大学四年,留给学弟学妹 -
libmw:
哎,剑光学长牛啊,刚刚在js讨论区看帖子,看着看着有个链接说正 ...
发现一个js replace正则相关的诡异问题 -
imuma:
通过Google搜索到了这篇文章,确实是非常好用的脚本。目前我 ...
分享一个自己写的httpWatch自动化测试工具 -
zcy860511:
。。。。。摇 头 丸替代品,牛叉
自己写了个《聊天颈椎保健小工具》
前几天帮erricwang写一个js计时器,发现可以用有很多种方式实现相同的功能。现将它写下来,和大家一起探讨分析比较。
实现一:
<body>
<div id="time1">time1</div>
<div id="time2">time2</div>
</body>
<script>
function Timer(id){
this.id = id;
this.begin = function(count){
this.show(this.id, count)();
setInterval(this.show(this.id, count-1),1000);
}
this.show = function(id, count){
return function(){
document.getElementById(id).innerHTML = count<0 ? "over" :count;
count--;
}
}
}
t1 = new Timer("time1");
t1.begin(50);
t2 = new Timer("time2");
t2.begin(10);
</script>
且看第一种实现,显然这里有一个很不好的地方,计时完成之后,setInterval没有被clear,严重影响了性能。
实现二:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; this.begin = function(count){ this.count = count; this.show(this)(); this.timer = setInterval(this.show(this),1000); } this.show = function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } } }
实现二解决了实现一的setInterval未被clear的问题,看上去也很“完美”,且看下面:
alert(t1.show == t2.show) //结果false alert(t1.begin == t2.begin) //结果false
很明显这里Timer每个实例都复制了一份show和begin,二不是共享同一份的,所以t1.showt和2.show所指向的并非同一份实现,这样对性能多少会有些影响。
实现三:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; this.begin = function(count){ this.count = count; Timer.show(this)(); this.timer = setInterval(Timer.show(this),1000); } Timer.show = function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } } }
这里实现了让所有实例共享一份show函数:
alert(t1.begin == t2.begin) //结果true
实现四:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; this.begin = function(count){ this.count = count; this.show(this)();//注意和实现三的区别:这里不是Timer.show(this)(); this.timer = setInterval(this.show(this),1000);//注意和实现三的区别:这里不是Timer.show(this)(); } } Timer.prototype.show = function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } }
实现三和实现四很有意思:二者都实现了让所有实例共享一份show方法。
区别是:实现三show作为Timer对象的一个属性,这里有点类似与java的静态方法,show方法是属于Timer对象本身的,而不是其实例(t1、t2...)的,所以需要用Timer.show(..)来调用;而实现四采用了“原型”prototype方式来实现,这里show方法不是Timer对象本身,而是其实例(t1、t2...)的,所以使用this.show(..)或者t1.show(...)来调用,这里很有意思“原型”创建的属性是属于其实例的,而且所有实例共享同一份实现(不知道我这里的理解是否正确,欢迎大家拍砖)。这多少有些让学java等面向对象语言的人难以理解,而这也正式js有意思的地方。
那么这两种实现方式那种更优呢?欢迎各位讨论。
实现五:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; } Timer.prototype.begin = function(count){ this.count = count; this.show(this)();//注意这里不是Timer.show(this)(); this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)(); } Timer.prototype.show = function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } }
这里将begin也使用原型实现,可能这种写法让你看起来不太舒服,那我们换一种。
实现六:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; } Timer.prototype = { begin : function(count){ this.count = count; this.show(this)();//注意这里不是Timer.show(this)(); this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)(); }, show : function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } } }
或者,再换一种。
实现七:
function Timer(id){ this.id = id; this.timer = null; this.count = 0; Timer.prototype.begin = function(count){ this.count = count; this.show(this)();//主要这里不是Timer.show(this)(); this.timer = setInterval(this.show(this),1000);//主要这里不是Timer.show(this)(); } Timer.prototype.show = function(obj){ return function(){ if(obj.count < 0){ document.getElementById(obj.id).innerHTML = "over"; clearInterval(obj.timer); return ; } document.getElementById(obj.id).innerHTML = obj.count; obj.count--; } } }
这方式,看起来是不是更优雅一些呢。以上都采用面向对象的方式来实现的。那我们是否还可以采用其他方式实现呢?
实现八:
var Timer = { begin : function(id,count){ var obj = {}; obj["id"] = id; obj["count"] = count; Timer.show(obj)(); obj["timer"] = setInterval(Timer.show(obj),1000);//注意这里不是Timer.show(this)(); }, show : function(obj){ return function(){ if(obj["count"] < 0){ document.getElementById(obj["id"]).innerHTML = "over"; clearInterval(obj["timer"]); return ; } document.getElementById(obj["id"]).innerHTML = obj["count"] ; obj["count"]--; } } } Timer.begin("time1", 30); Timer.begin("time2", 60);
这里采用了对象字面量的方式来实现的。对象字面量其实是一种单例模式,用在这里其实很变扭,本文这里引入只是引入一种js实现方式而已,仅供大家探讨。下一例也一样。
实现九:
var Timer = (function(){ var items = {}; function begin(id,count){ var obj = {}; obj["id"] = id; obj["count"] = count; Timer.show(obj)(); obj["timer"] = setInterval(Timer.show(obj),1000);//注意这里不是Timer.show(this)(); Timer.items[id] = obj; }; function show(obj){ return function(){ if(obj["count"] < 0){ document.getElementById(obj["id"]).innerHTML = "over"; clearInterval(obj["timer"]); return ; } document.getElementById(obj["id"]).innerHTML = obj["count"] ; obj["count"]--; } } return { items : items, begin : begin, show : show } })() Timer.begin("time1", 30); Timer.items["time1"]["count"] = 80;//重新从80开始计时 Timer.begin("time2", 60);
这里其实也是采用的对象字面量的方式来实现的,只是采用了闭包而已,应用闭包可以真正的实现属性私有化(不过这里没有体现出来)。这里还加了items属性,让其所有实例保存起来,以便后面还可以调用。
平常用的比较多的还有最后一种闭包结合对象字面量的方式(不过在这个场景个人觉得用面向对象方式来实现更好一些)。对象字面量的方式其实也是js的单例模式,在js里应用很广泛,不过在这里应用好像不太合适,看这里的最后两个实现都觉得很牵强。
从一个小小的计时器,可以衍生出如此多的方式来实现,真让人惊叹于js表现形式之丰富。如此众多实现方式,都有其优点和缺点,在不同的场合也有不同的应用。这里只是表达了一下个人的理解和观点,欢迎大家一起讨论js各种实现方式的优缺点及其适用场合。也欢迎拍砖,或者提供更好的实现方法。
【本人发帖抛砖引玉,希望能够引出更多的“玉”来,希望所写的每一段代码都能够得到一种最“优雅”的实现方式。以后本人会抛出更多的“砖”,希望能引来更多的“玉”,以供大家一起学习进步】
-------------------------------------------------------2009.08.17------------------------------------------------------------
到目前为止,个人觉得3楼zbm2001 的实现最优,让我学到了不少,希望能有更多的牛人来提供更优的实现。
function Timer(id){ this.element = document.getElementById(id); this.timer = null; this.count = 0; } Timer.prototype = { begin : function(count){ this.count = count; this.show(); var _this = this; this.timer = setInterval(function(){_this.show();}, 1000); } , show : function(){ this.element.innerHTML = this.count < 0 ? clearInterval(this.timer) || "over" : this.count--; } }
-------------------------------------------------------2009.08.21------------------------------------------------------------
继续收集评论中的实现方式,优劣请读者自评
var Timer = function(id) { var _step = 500, _count = 0, _ticktark = 0, _element = document.getElementById(id); function __clear() { if (_ticktark != null) clearInterval(_ticktark); } return { begin: function(count, step) { if (_element && count > 0) { // 看看起始值多少,主要看看有没有被污染 console.log('on start:', 'count:', _count, 'step:', _step); __clear(); _step = step; _count = count; // 再看看 console.log('on set:', 'count:', _count, 'step:', _step); _ticktark = setInterval(this.show, _step) } return this; }, show: function() { _element && (_element.innerHTML = _count > 0 ? _count-- : 'over'); if (_count <= 0) { console.log(_count); __clear(); } return this; } } } Timer('time1').begin(20, 100); Timer('time2').begin(30, 200);
function Timer(id) { this.container = document.getElementById(id); } Timer.prototype = { constructor: Timer, begin: function(count) { var container = this.container; setTimeout(function() { container.innerHTML = count > 0 ? count-- : "over"; if(count + 1) { setTimeout(arguments.callee, 1000); } }, 1000); } }; new Timer("time1").begin(10);
评论
function Timer(count,id) { var start=count,obj=document.getElementById(id); do { (function (count) { setTimeout(function () { obj.innerHTML=count; },(start-count)*1000); })(count); } while (count--); }
一个函数,不做构造了,直接调用就执行
Js代码
Timer(10,"oDiv");
function Timer(count,id) { var start=count,obj=document.getElementById(id); do { (function (count) { setTimeout(function () { obj.innerHTML=count; },(start-count)*1000); })(count); } while (count--); }
一个函数,不做构造了,直接调用就执行
Timer(10,"oDiv");
<div class="quote_div">
<p>放在Timer构造函数内部的话:</p>
<p> </p>
<pre name="code" class="js">function Timer(id){
this.id = id;
this.timer = null;
this.count = 0;
Timer.prototype = {
begin : function(count){
//alert(this);
this.count = count;
this.show(this)();//注意这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)();
},
show : function(obj){
return function(){
if(obj.count < 0){
document.getElementById(obj.id).innerHTML = "over";
clearInterval(obj.timer);
return ;
}
document.getElementById(obj.id).innerHTML = obj.count;
obj.count--;
}
}
};
} </pre>
<p> </p>
很奇怪,不知道什么原因
<p> </p>
</div>
<p>其实只要把例子构造得稍微复杂点的例子,就很容易理解这个问题了</p>
<p> </p>
<pre name="code" class="js">function Timer(id){
this.id = id;
this.timer = null;
this.count = 0;
this.begin=function(count){
alert('this:'+id);
this.count = count;
this.show(this)();//注意这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)();
}
Timer.prototype = {
id:"abcd",
begin : function(count){
alert('prototype');
this.count = count;
this.show(this)();//注意这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)();
},
show : function(obj){
return function(){
if(obj.count < 0){
document.getElementById(obj.id).innerHTML = "over";
clearInterval(obj.timer);
return ;
}
document.getElementById(obj.id).innerHTML = obj.count;
obj.count--;
}
}
};
}
new Timer('aaa').begin(10);</pre>
// 声明一个类(其实也就是对象),其中initialize是构造方法
function MyObj(){this.initialize.apply(this, arguments);}
// 类的属性和方法声明
MyObj.prototype = {
attr1 : null, // 属性,当然也可以在方法中直接this.attr1=来设置
// 构造方法,可以直接new MyObje(args)
initialize : function() {
},
// 一般方法
func1 : function() {
}
}
// 类的静态方法
MyObj.func2 = function() {
};
<div class="quote_div">
<div class="quote_title">
<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/"></a>clone168 写道</div>
<div class="quote_div">
<p><span>作者js功力确实不错,可惜俺有一个疑问(具体<span style="background-color: #ffffff;">见</span><span><span style="font-weight: normal;"><span style="text-decoration: none;"><span style="font-size: small;"><span style="color: #000000;"><span style="background-color: #ffffff;"><a class="quote_title" style="color: white; background-color: #108ac6;" title="js一个很奇怪的问题" href="http://clone168.iteye.com/blog/452290" target="_blank">js一个很奇怪的问题</a><span>),当我把“代码六”中的</span></span></span></span></span></span></span></span></p>
<p> </p>
很奇怪,不知道什么原因
<p> </p>
</div>
<p> </p>
<p>是因为 new Timer(id) 的内部机制里,请参考:<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/">http://lifesinger.org/blog/2009/08/new-funtion-secret/</a></p>
</div>
<p> </p>
<p> </p>
<p>lifesinger牛人,从内部机制来分析,分析的很细,让我学习了不少。从内部来分析,需要对内部非常了解。因此看起来比较困难。</p>
<p>方式A:</p>
<pre name="code" class="java">function Dog() {
alert(this.__proto__.name); // undefined
Dog.prototype.name = 2; //方式A (注意这里)
alert(this.__proto__.name); // 2 (注意这里)
}
var dog1 = (function(){
var o = {__proto__: Dog.prototype};
alert(Dog.prototype.name);// undefined
alert(o.__proto__.name); // undefined
Dog.apply(o);
alert(Dog.prototype.name) //2
alert(o.__proto__.name); //2
return o;
})()
alert(dog1.__proto__.name) // 2
alert(dog1.name) // 2</pre>
<p> 方式B:
</p>
<pre name="code" class="js">function Dog() {
alert(this.__proto__.name); // undefined
Dog.prototype = {name : 2}//方式B (注意这里)
alert(this.__proto__.name); // undefined (注意这里)
}
var dog1 = (function(){
var o = {__proto__: Dog.prototype};
alert(Dog.prototype.name);// undefined
alert(o.__proto__.name); // undefined
Dog.apply(o);
alert(Dog.prototype.name) //2
alert(o.__proto__.name); //undefined
return o;
})()
alert(dog1.__proto__.name) // undefined
alert(dog1.name) // undefined</pre>
<p>化简后的问题:</p>
<pre name="code" class="js">function Base(){
Base.prototype.name = 2; //方式A (注意这里)
alert(this.a.name) //2(注意这里)
}
var o = {a : Base.prototype};
Base.apply(o);
alert(o.a.name);// 2(注意这里)
function Base2(){
Base2.prototype = {name : 2}; //方式B(注意这里)
alert(Base2.prototype.name) //2
alert(this.a.name) //undefined(注意这里)
}
var o2 = {a : Base2.prototype};
Base2.apply(o2);
alert(o2.a.name);// undefined(注意这里)</pre></div><br/>楼上例子简明易懂,赞一个
<div class="quote_div">
<div class="quote_title">
<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/"></a>clone168 写道</div>
<div class="quote_div">
<p><span>作者js功力确实不错,可惜俺有一个疑问(具体<span style="background-color: #ffffff;">见</span><span><span style="font-weight: normal;"><span style="text-decoration: none;"><span style="font-size: small;"><span style="color: #000000;"><span style="background-color: #ffffff;"><a class="quote_title" style="color: white; background-color: #108ac6;" title="js一个很奇怪的问题" href="http://clone168.iteye.com/blog/452290" target="_blank">js一个很奇怪的问题</a><span>),当我把“代码六”中的</span></span></span></span></span></span></span></span></p>
<p> </p>
很奇怪,不知道什么原因
<p> </p>
</div>
<p> </p>
<p>是因为 new Timer(id) 的内部机制里,请参考:<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/">http://lifesinger.org/blog/2009/08/new-funtion-secret/</a></p>
</div>
<p> </p>
<p> </p>
<p>lifesinger牛人,从内部机制来分析,分析的很细,让我学习了不少。从内部来分析,需要对内部非常了解。因此看起来比较困难。</p>
<p>方式A:</p>
<pre name="code" class="java">function Dog() {
alert(this.__proto__.name); // undefined
Dog.prototype.name = 2; //方式A (注意这里)
alert(this.__proto__.name); // 2 (注意这里)
}
var dog1 = (function(){
var o = {__proto__: Dog.prototype};
alert(Dog.prototype.name);// undefined
alert(o.__proto__.name); // undefined
Dog.apply(o);
alert(Dog.prototype.name) //2
alert(o.__proto__.name); //2
return o;
})()
alert(dog1.__proto__.name) // 2
alert(dog1.name) // 2</pre>
<p> 方式B:
</p>
<pre name="code" class="js">function Dog() {
alert(this.__proto__.name); // undefined
Dog.prototype = {name : 2}//方式B (注意这里)
alert(this.__proto__.name); // undefined (注意这里)
}
var dog1 = (function(){
var o = {__proto__: Dog.prototype};
alert(Dog.prototype.name);// undefined
alert(o.__proto__.name); // undefined
Dog.apply(o);
alert(Dog.prototype.name) //2
alert(o.__proto__.name); //undefined
return o;
})()
alert(dog1.__proto__.name) // undefined
alert(dog1.name) // undefined</pre>
<p>化简后的问题:</p>
<pre name="code" class="js">function Base(){
Base.prototype.name = 2; //方式A (注意这里)
alert(this.a.name) //2(注意这里)
}
var o = {a : Base.prototype};
Base.apply(o);
alert(o.a.name);// 2(注意这里)
function Base2(){
Base2.prototype = {name : 2}; //方式B(注意这里)
alert(Base2.prototype.name) //2
alert(this.a.name) //undefined(注意这里)
}
var o2 = {a : Base2.prototype};
Base2.apply(o2);
alert(o2.a.name);// undefined(注意这里)</pre>
<div class="quote_div">
<div class="quote_title">
<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/"></a>clone168 写道</div>
<div class="quote_div">
<p><span>作者js功力确实不错,可惜俺有一个疑问(具体<span style="background-color: #ffffff;">见</span><span><span style="font-weight: normal;"><span style="text-decoration: none;"><span style="font-size: small;"><span style="color: #000000;"><span style="background-color: #ffffff;"><a class="quote_title" style="color: white; background-color: #108ac6;" title="js一个很奇怪的问题" href="http://clone168.iteye.com/blog/452290" target="_blank">js一个很奇怪的问题</a><span>),当我把“代码六”中的</span></span></span></span></span></span></span></span></p>
<p> </p>
很奇怪,不知道什么原因
<p> </p>
</div>
<p> </p>
<p>是因为 new Timer(id) 的内部机制里,请参考:<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/">http://lifesinger.org/blog/2009/08/new-funtion-secret/</a></p>
</div>
<p>射雕的文章我看了,只可惜自己还是感觉不太明白:</p>
<p>
</p>
<div class="quote_title">射雕 写道</div>
<div class="quote_div">// Dog.prototype = {constructor: Dog};<br>var o = {__proto__: Dog.prototype};<br>// 此时,o = {__proto__: {constructor: Dog}}<br>Dog.apply(o);<br>// 此时,Dog.prototype = {shout: function(){...}}<br>return o;<br>显然,运行 dog1.shout() 时,dog1 的确没有 shout 方法。</div>
<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/"></a>clone168 写道</div>
<div class="quote_div">
<p><span>作者js功力确实不错,可惜俺有一个疑问(具体<span style="background-color: #ffffff;">见</span><span><span style="font-weight: normal;"><span style="text-decoration: none;"><span style="font-size: small;"><span style="color: #000000;"><span style="background-color: #ffffff;"><a class="quote_title" style="color: white; background-color: #108ac6;" title="js一个很奇怪的问题" href="http://clone168.iteye.com/blog/452290" target="_blank">js一个很奇怪的问题</a><span>),当我把“代码六”中的</span></span></span></span></span></span></span></span></p>
<p> </p>
很奇怪,不知道什么原因
<p> </p>
</div>
<p> </p>
<p>是因为 new Timer(id) 的内部机制里,请参考:<a href="http://lifesinger.org/blog/2009/08/new-funtion-secret/">http://lifesinger.org/blog/2009/08/new-funtion-secret/</a></p>
setInterval有可能线程阻塞
孰优孰劣还是要看具体情况吧
的确如此。
对于复杂页面,线程阻塞比内存开销更恐怖,作为一个良好的编码习惯,推荐多用setTimeout
<p> </p>
<pre name="code" class="js">Timer.prototype = {
begin : function(count){
this.count = count;
this.show(this)();//注意这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)();
},
show : function(obj){
return function(){
if(obj.count < 0){
document.getElementById(obj.id).innerHTML = "over";
clearInterval(obj.timer);
return ;
}
document.getElementById(obj.id).innerHTML = obj.count;
obj.count--;
}
}
} </pre>
<p> </p>
<p> 放在Timer构造函数内部的话:</p>
<p> </p>
<pre name="code" class="js">function Timer(id){
this.id = id;
this.timer = null;
this.count = 0;
Timer.prototype = {
begin : function(count){
//alert(this);
this.count = count;
this.show(this)();//注意这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//注意这里不是Timer.show(this)();
},
show : function(obj){
return function(){
if(obj.count < 0){
document.getElementById(obj.id).innerHTML = "over";
clearInterval(obj.timer);
return ;
}
document.getElementById(obj.id).innerHTML = obj.count;
obj.count--;
}
}
};
} </pre>
<p> </p>
<p> <span style="font-family: Arial; line-height: 18px;"><span style="">在</span><span lang="EN-US">firebug</span><span style="">下面提示:“</span><span class="objectboxobjectbox-errormessagehastwistyhasbreakswitchopened"><span lang="EN-US">t1.begin is not a function</span></span><span style="">”这样的错误,<span style="font-family: Arial;"><span>而且,看到“代码七”就发现,都用</span><span lang="EN-US">Timer.prototype.begin</span><span>和</span><span lang="EN-US">Timer.prototype.show</span><span>就算写在里面也能运行:</span></span></span></span></p>
<p>
</p>
<pre name="code" class="js">function Timer(id){
this.id = id;
this.timer = null;
this.count = 0;
Timer.prototype.begin = function(count){
this.count = count;
this.show(this)();//主要这里不是Timer.show(this)();
this.timer = setInterval(this.show(this),1000);//主要这里不是Timer.show(this)();
}
Timer.prototype.show = function(obj){
return function(){
if(obj.count < 0){
document.getElementById(obj.id).innerHTML = "over";
clearInterval(obj.timer);
return ;
}
document.getElementById(obj.id).innerHTML = obj.count;
obj.count--;
}
}
} </pre>
很奇怪,不知道什么原因
<p> </p>
setInterval有可能线程阻塞
孰优孰劣还是要看具体情况吧
function Timer(id) { this.container = document.getElementById(id); } Timer.prototype = { constructor: Timer, begin: function(count) { var container = this.container; setTimeout(function() { container.innerHTML = count > 0 ? count-- : "over"; if(count + 1) { setTimeout(arguments.callee, 1000); } }, 1000); } }; new Timer("time1").begin(10);
也来一个,补充两点:
1. prototype 的 constructor 不要搞丢了
2. 用 setTimeout,比 setInterval 好
js太灵活了. 以至于我无法驾驭.. 不过支持剑光.
1 ,这2个想当然尔的理论是从一些书上看到的,比如月影的王者归来,月影做为我眼里这个理论的发源地,相信他会为此负责的.
你难道总是希望别人为你的代码负责?
只可惜宁愿迷信所谓某人、某书、某权威,也不愿写几行代码测试一下。
……john resig大人也说他真正理解闭包花去了相当多的精力和时间.
既然你已知道john resig都花去了相当多的精力和时间才真正理解闭包,所以你应该明白我所说的——你的结论在这(真正理解闭包)之后再下吧:
……
如果你一直坚持尝试利用闭包来简化你的开发(虽然可能曾经会饱受挫折),并结合着不断阅读直到完全理解下文,再来回顾上面的结论看看……
不过对于一个对某些结论连几行测试代码都懒的写,还声称自有别人为这些结论负责的程序员
——估计也不能指望比john resig花更多的的精力和时间了!
这个想当然尔的理论从哪里来?
你随便一说,也不知道谁会受到误导,测试几个主流浏览器出个数据再说吧。
1. 可能存在的内存泄露.
2. 让scope chains难以维护.
3. 程序可读性降低,闭包引起的bug经常叫人抓狂.
其实闭包不是个很神秘的东西,没有必要想法设法跟它扯上关系.不过楼主的研究精神狠赞一个.
如果你一直坚持尝试利用闭包来简化你的开发(虽然可能曾经会饱受挫折),并结合着不断阅读直到完全理解下文,再来回顾上面的结论看看……
中译文:
http://www.cn-cuckoo.com/2007/08/01/understand-javascript-closures-72.html
原文:
http://www.jibbering.com/faq/faq_notes/closures.html
是啊,发现javaeye质量比csdn高好多哦
发表评论
-
写了个chrome的QQ会员扩展
2010-02-10 15:25 1736今天在公司放羊,没事干,玩了下chrome的插件开发, ... -
IE6下动态加载JS数据不执行的诡异问题
2009-12-07 19:00 1399最近遇上个非常诡异的问题:在ie6下,从地址栏直接输入网址,或 ... -
自己写了个《聊天颈椎保健小工具》
2009-11-29 22:59 3064和亲爱的聊天的时候,你有没想过去关心下她的健康呢,用这个工具可 ... -
jWidget:js slide轮播、tab页签
2009-10-31 22:13 2780jWidget: a mini javascript widg ... -
IE6下gif动画静止的问题
2009-09-09 23:32 2057今天hairetzhang做头像设置的时候遇到gif动画被点击 ... -
对js运算符“||”和“&&”的总结
2009-09-04 22:31 45998首先出个题: 如图: 假设对成长速度显示规定如下: ... -
从帖子《可输入的select下拉框》的优化,总结js键盘事件的兼容性
2009-08-23 23:41 3756本文源起javaeye的论坛帖子《可输入的select下拉框 ... -
答复: js 实现下拉列表的重复利用
2009-08-21 23:23 1015原帖《js 实现下拉列表的重复利用 》 可以优化一下代码: ... -
【JS优化系列】一个通用公告系统的实现
2009-08-17 22:22 1241之前一篇《【JS优化系列】从一个计时器的写法探讨 ... -
制作IE扩展
2009-06-20 20:29 1322上周终于把有我全权负 ... -
分享一个自己写的httpWatch自动化测试工具
2009-06-20 21:02 56242009-04-22 分享一个自己写的httpWatch ... -
发现一个js replace正则相关的诡异问题
2009-07-17 09:43 4960最近做会员个人身份页的时候遇上如下问题: 测试的 ... -
表单元素命名需要注意的一个地方:不要使用与form属性同名的name或id
2009-07-31 20:21 3308最近做官方点充值入口的时候遇到个奇怪的问题。 直接 ...
相关推荐
js实现计时器,只需要将设定的开始时间修改为你需要的开始时间就可以使用,制作个人网站,纪念重要日期非常实用。
自己实现的简单的倒计时器,发现网上关于这方面的代码有...我自己用js手写了一个分享给大家,界面比较简洁,后面会再写一个比较酷炫的。如果没有积分还想下载的小伙伴可以加qq邮箱联系我,我发给你:1343121616@qq.com
附带三种计时器实现,第三种是最好的。前两种是调研的中间结果,直到发现第三种。
js css3圆形的时分秒计时器动画特效 js css3圆形的时分秒计时器动画特效 js css3圆形的时分秒计时器动画特效 js css3圆形的时分秒计时器动画特效
JS实现一个秒表计时器 本文实例为大家分享了JS实现秒表计时器的具体代码,供大家参考,具体内容如下 秒表计时器的实现: 效果图如下: 附代码,已调试运行 <!DOCTYPE html> <html lang="en"> <head> ...
这是一款css3圆形进度条时分秒计时器js特效,非常简洁又漂亮的网页计时动画代码。
javascript实现倒计时
使用javascript实现的计时器秒表,使用setTimeout()来实现
js计时器,功能有:开始计时,暂停计时器,停止计时器
JS CSS3实现苹果iwatch计时器,带有开始和重置按钮,支持计次显示。
javascript 实现网页 倒计时 代码
主要介绍了python实现简单的计时器功能函数,涉及Python操作时间的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
javascript秒表计时器,可以js控制。
js计时器(setInterval)实现,暂停开始,粘贴赋值可用
一个自己写的JS计时器,供大家看看,请高手给点评一下,供小弟学习,谢谢!
js 倒数计时器 counttimedown javascript 写的时间倒数计时器
Android实现一个计时器。功能:计时,可以暂停计时。主要利用handler的postdelay()函数。
倒计时功能插件js
理想Web倒计时器的设计与实现
实现简单计时器的功能 界面设计有开始按钮,暂停,清零三个按钮。