原文:http://arstechnica.com/journals/linux.ars/2007/08/27/javascript-for-all-ages?foo
1、objects and associative arrays
javascript中,对象属性操作与数组很类似。
但 javascript也为我们做了一些工作,把 数字转化为字符串。不过还是建议自己写清楚
foo[1] == foo[(1).toString()] == foo['1'];
2、cute type conversions
这些基本我们都知道。
也可看看
http://www.jibbering.com/faq/faq_notes/type_convert.html
0 + '' == 0
'' + 0 == 0
1 + '' == 1
'' + 1 == 1
0 + 'a' == '0a'
'a' + 0 == 'a0'
但这个技巧呢?很多lib里面都用了。
var a=function(){}
var b=new a();
alert(!b)
alert(b)
3、iteration
prototype.js,jQuery。n多的FP的东西。
4、anonymous functions
也见 http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6
yield,反正我自己用起来很别扭。
喜欢ruby的yield.
var range = function (start, stop, step) {
return function () {
var at = start;
start += step;
if (at < stop) return at;
else throw new Error();
};
};
var next = range(0, 6, 2);
alert(next())//0
alert(next())//2
alert(next())//4
大量的应用”闭包“,始终感觉性能上有些问题。可能是个人感觉
5、enclosure
(function () {
...
}).call(this);
语言的”最小单元“,不知道该叫什么。但是java里面,所谓”最小单元“,也是一个类。而javascript或ruby之类的,一断代码就可以,可以通过上面的方法动态注入执行环境。我不是很喜欢javascript的eval的功能。
with也有类似功效。好象很多书都不建议用with。性能慢是一个原因。感觉javascript语言不想让代码编写者了解“scope chain”。所以闭包,with,eval等等都不是很流行。可能这方面javascript设计的不是很严谨???
6、context object manipulation
Function.apply
Function.call
在这里很关键,
在java中,那么多设计模式都是关于接口的,我理解为java中接口是最小单元,如果没有接口,你根本不知道那些方法的context object是什么,也不可能执行方法。
而javascript呢?有function就好了,注意this都ok了。
读了jQuery代码后,在这方面有很多感想,有时间具体分析一下。
7、variadic arguments
小tip。
8、binding
与第六差不多。
跑题:
javascript模拟java的面向对象编程时,其实也模拟的java的”缺点“,状态保存的位置!!!
var Button=function (dom){
this.**=**;
this.**=**;
}
这种”类“其实完全没有必要的!!!
9、lazy function definition
大家讨论不少了!
相同的功能,其实YUI代码里早用到了。
http://developer.yahoo.com/yui/docs/Dom.js.html
见getStyle setStyle 等。
var getPi = function () {
var pi = calculatePi();
getPi = function () {
return pi;
}
return pi;
};
我到是不认为它这例子好。真正这些功能都在lib中实现,象cache,function覆盖等!
10、polymorphic callable objects
新的想法,区别java之类的多态
var callableType = function (constructor) {
return function () {
var callableInstance = function () {
return callableInstance.callOverload.apply(callableInstance, arguments);
};
constructor.apply(callableInstance, arguments);
return callableInstance;
};
};
var SubmarineFactory = type(function () {
var length;
this.init = function (length) {
length = lengthValue;
};
this.callOverload = function () {
return Submarine(length);
};
});
分享到:
相关推荐
一个基于ASP.NET 的js客户端处理技术JavaScript Tips 之一。
一个基于ASP.NET 的js客户端处理技术JavaScript Tips 之二。
Javascript 编程技巧 易学易懂
今天看到《ASP小贴士/ASP Tips》 我也去把JavaScript的tips 下下来了。 看看是A4的。 自己把他改成1024 * 768 刚好可以用来做桌面
一个简单的类似title的提示效果,但现实内容可以很丰富,以上js另存为tip.js,下面是使用的demo。
大家在使用JavaScript的时候,DOM操作是最平常不过的了, 随着Web前端技术的发展,我们越来越多的使用JS来操作DOM元素,比如通过ajax请求获取到数据,然后更新页面上的元素,一般情况下,这种操作我们会用类似node....
The.JavaScript.Anthology.101.Essential.Tips.Tricks.Hacks
The HTML5 Programmer’s Reference aims to provide everything a programmer needs for understanding and using the new ...Appendix A: JavaScript Tips and Techniques
Javascript技巧有缺陷的数学0.1 + 0.2!= 0.3 0.1 + 0.2 == 0.30000000000000004 console.log(0.1+0.2);简单的变量可以被复制并将是新的 var x = 1;var y = x;x = 2;console.log(x,y);结果:x = 2,y = 1复杂变量...
This course will begin with providing insights and practical tips on advanced JavaScript features to build highly scalable web and mobile system and move on to some design patterns with JavaScript....
Like it or not, JavaScript is everywhere these days—from browser to server to mobile—and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and ...
You’ll learn to build several different apps, and along the way you’ll pick up a ton of useful advice, tips, and techniques. It contains: Build a Full-Sphere 3D Image Gallery with React VR by ...
很经典的JavaScript教程。
JavaScript Performance Optimization Tips: An Overview by Ivan CuriC JavaScript Design Patterns: The Singleton by Samier Saeed JavaScript Object Creation: Patterns and Best Practices by Jeff Mott Best ...
Get an overview of HTML5, JavaScript, and mobile web frameworks Discover tips for iOS usability as well as performance Dig into features like images, animation, and even geolocation
Create Ajax-enabled tool tips Use XPath and XSLT on XML documents loaded with Ajax And much more Part III is a complete reference for core JavaScript. It documents every class, object, ...