朋友推荐的一本书(http://eloquentjavascript.net/),趁最近不忙看了下。

总的来说这本书一般吧,不大适合JS入门读者,因为里面的例子比较败笔,比较学术性不够生动和切符实际工作应用。

对于JS的书,个人还是推荐《head first javascript》~事实上据说head first那系列的书都还不错。

每本书总有其可取之处的,如果你计划也看这本书,或许你直接过一下我的笔记好了。。。

注:英文为原文,中文为我的注释。没有英文的中文是我直接翻译过来的。

 

1,There are six basic types of values: Numbers, strings, booleans, objects, functions, and undefined values.

  6种值的基本类型

 

2,Don't forget the special unary operator (typeof)

  容易被遗忘的一元操作符typeof

 

3,js里面,语句结束处一般带分号,有时可以忽略,有时是必须的。强烈建议保留分号。

  (读者注:习惯性保留分号,在使用yui压缩器压缩Js时不容易出错)

 

4,undefined和null。

  变量值为null可以认为该变量定义了但是没值;undefined则压根儿没定义。但是null==undefined返回true。所以在js里面,如果变量要和null或者undefined比较,最好还是用===或!==

 

5,JS里面其他比较诡异的现象。

  var x=false == 0;var y="" == 0;var z="5" == 5;xyz全部为true

  读者注:其实有很多诡异的现象

 

6,精确等于以及精确不等于 === and !==

  利用==和!=进行比较时,JS解释器会尝试进行类型转换(例如5中的一些怪异现象),如果在JS里面比较时你看不惯它自动的类型转换,那就用=== 和 !==

 

7,猜猜NaN== NaN等于啥?false...


 

8,||和&&操作符的妙用。true||x,x将永远不会被执行;同理false&&y,y也永远不会被执行

  ||经常用,&&的这种用法还真没亲自试过
 


 

9,没有return语句的方法返回什么?

  undefined

 

10,Functions are the only things that create a new scope

  作用域~这句话的意思是,在block型的语句内(例如if,while)生命变量时,变量的作用域其实是block语句所属的函数。这点和c#差异比较大,据说新版的javascript会改变这点。

 

11,In JavaScript, running through a simple loop is a lot cheaper than calling a function multiple times.
    简单的for或者while循环,性能要比函数的递归调用好不少。但是如果逻辑复杂该用递归还是得用,性能要考虑但不用过分担忧性能,除非你的程序真的慢的不行。

 

12,the computer must remember the context from which the function was called, so that it knows where to continue afterwards.

The place where this context is stored is called the stack.

    堆栈是存储函数运行时的上下文的地方。每当一个函数被调用时,和它相关的上下文将位于堆栈的头部,函数调用结束后,上下文将从堆栈上撤掉,同时继续执行处理堆栈中剩余的函数的上下文。
    堆栈的物理性表现在电脑内存的占用上。如果堆栈过大,电脑便会抛出“堆栈溢出out of stack space”或者“过多递归too much recursion”的信息。所以,一旦碰到这两提示,记得检查程序中哪个地方有递归调用导致死循环。
    jQuery中的animate方法结合stop方法使用时容易导致死循环。如果你对元素$x调用了有回调函数的animate如$x.animate({xxx},400,callback),然后在程序的另外一个地方调用了$x.stop(true,true),这时候得注意你的callback函数。

 

13,Numbers, booleans, the value null, and the value undefined do not have any properties

数字、布尔值、null和undefined没有任何属性

 

14,The keyword delete cuts off properties

delete关键字用来删除对象的属性

 

15,The operator in can be used to test whether an object has a certain property. It produces a boolean

in关键字可用来检测某个对象是否具有指定属性。
如var levin={name:'levin'};
alert("name" in levin);

 

16,string.split(x).join(x) always produces the original value, but array.join(x).split(x) does not.


 

17,charAt will return "" when there is no character at the given position, and slice will simply leave out the part of the new string that does not exist.


 

18,the properties of Math are hidden

for (var name in Math)
  print(name);
啥也没发生

 

19,The simple objects we have used so far are based on the most basic prototype, which is associated with the Object constructor. In fact, typing {} is equivalent to typing new Object()

原型对象与字面对象-原型对象的constructor属性指向创建该对象的类的构造函数;字面对象的constructor属性指向Object类的构造函数

读者注:可以参考我的例子:http://www.vivasky.com/labs/jqfocus/js_PrototypeBasedObject_vs_LiteralObject.html

 

20,Every function automatically gets a prototype property, whose constructor property points back at the function

任何类(Class)都有一个原型(prototype)属性,该属性的构造器(constructor)属性指向声明该类的函数自身!

 

21,The properties of the prototype influence the object based on it, but the properties of this object never change the prototype

    类的原型的属性影响该类的实体,但是类实体的属性是不会改变类原型的。
    var Baby=function(name){this.name=name;};
    Baby.prototype.age=1;

 

    var baby1=new Baby("cocoa");
    baby1.age=2;

    alert(Baby.prototype.age); /* still shows 1 */

 

22,if your program has to run on the same web-page as another program (either written by you or by someone else) which uses for/in naively ― the way we have been using it so far ― then adding things to prototypes, especially the Object and Array prototype, will definitely break something, because these loops will suddenly start seeing those new properties. For this reason, some people prefer not to touch these prototypes at all. Of course, if you are careful, and you do not expect your code to have to coexist with badly-written code, adding methods to standard prototypes is a perfectly good technique.

尽量避免对JS本地类如Object、Array等进行原型扩展。

读者注:在工作中遇到过此类问题。

 

23,Modularity
When structuring a program, we do two things. We separate it into smaller parts, called modules, each of which has a specific role, and we specify the relations between these parts.

JS编程的组织-模块化开发
按功能分模块,每个模块负责制定的角色(功能)。模块之间的通信方式是关键的地方,这个可以借鉴java或者c#里面的事件机制。如果你在用jquery的话比较好处理,因为它提供了一套完善的事件处理机制。

 

在组织一个程序的各个模块时,要尽量避免模块间循环依赖,比如a依赖b,b反过来也依赖a的话容易导致逻辑混乱。
JQuery从1.4.3版本开始也采用模块化开发,比如处理效果的功能放到effects.js模块,处理dom遍历的放在core.js等等,由于模块间尽量保持独立,用户就可以根据需要构建自己专用的jquery版本。

 

模块化开发时,建议将各模块物理分离到不同的js文件中,部署时再使用yui压缩工具合并成一个js文件。

作者: Mamboer 发表于 2010-12-15 10:47 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"