require.js
promise模式, jquery 的deffered对象。
use strict(使用严格模式) 符合很多的条件限制。
javascript的编译时和运行时, var function的概念有待追求清楚。
var keyword in javascript:
If you're in a function then "var" will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it):
eval()函数的使用:
优点: 动态生成javascript代码。
(在某些场合,你不确定需要执行的具体代码,比如某个根据某元素的ID来对一个元素进行操作,但是这个ID确实生成的,不是预先知道的。)
缺点:
-
Improper use of eval opens up your code for injection attacks
-
Debugging can be more challenging (no line numbers, etc.)
-
eval'd code executes more slowly (no opportunity to compile/cache eval'd code)
this关键字在javascript中的理解:
You can figure out what object this
refers to by following a few simple rules:
- By default,
this
refers to the global object. - When a function is called as a property on a parent object,
this
refers to the parent object inside that function. - When a function is called with the
new
operator,this
refers to the newly created object inside that function. - When a function is called using
call
orapply
,this
refers to the first argument passed tocall
orapply
. If the first argument isnull
or not an object,this
refers to the global object.
sample for understanding:
http://stackoverflow.com/questions/8813871/understanding-this-in-javascript
http://net.tutsplus.com/tutorials/javascript-ajax/fully-understanding-the-this-keyword/
use strict的使用:
使用use strict的目的
1: 消除不合理的地方, 使代码更合理。
例子: 变量重名将报错而不是覆盖上一个,变量必须显示声明而不是默认为全局变量。
2: 更快,因为javascript允许动态绑定,所以可以在运行时才确定方法,变量。
strict模式对一些情况不允许动态绑定,必须静态绑定,可以提高执行效率。
例子:禁止使用with语句,创建eval的变量作用域。
3:更加安全
例子:严格模式下无法删除变量。只有configurable设置为true的对象属性,才能被删除
使用构造函数时,如果忘了加new,this不再指向全局对象,而是报错。