Jan*_*nak 6 javascript function definition user-defined-functions hoisting
它们之间有什么区别吗?我一直在使用这两种方式,但不知道哪一种做什么,哪种更好?
function abc(){
// Code comes here.
}
abc = function (){
// Code comes here.
}
Run Code Online (Sandbox Code Playgroud)
定义这些功能有什么区别吗?像i ++和++ i这样的东西?
function abc(){
// Code comes here.
}
Run Code Online (Sandbox Code Playgroud)
将被悬挂.
abc = function (){
// Code comes here.
}
Run Code Online (Sandbox Code Playgroud)
不会被吊起来.
例如,如果您这样做:
abc();
function abc() { }
Run Code Online (Sandbox Code Playgroud)
代码将按原样运行abc
到封闭范围的顶部.
如果你做了:
abc();
var abc = function() { }
Run Code Online (Sandbox Code Playgroud)
abc
已声明但没有价值,因此无法使用.
至于哪个更好是更多关于编程风格的争论.
http://www.sitepoint.com/back-to-basics-javascript-hoisting/
归档时间: |
|
查看次数: |
100 次 |
最近记录: |