What does this structure in Javascript mean?
Im learning javascript and I just dont understand what this javascri开发者_JAVA百科pt syntax actually means...
Comment = function () {
}
I know that in this context we use it to define an object but what is the stucture called where can I read about it?
This statement is a function expression.
It creates an anonymous function and assigns it to the (global) variable Comment
.
It is similar to function Comment() {}
except one difference: It does not have a name so debuggers might just display ?
instead of a function name and more important, the function is defined when the line containing the definition is executed while function declarations are "executed" before the other code is executed.
Example: http://jsfiddle.net/ThiefMaster/nVrep/
You are defining a function named Comment
, this is equivalent to:
function Comment() {
}
Because there is now var
keyword like here:
var Comment = function() {}
the Comment
variable will be added to global object called window
, so it is equivalent to:
window.Comment = function() {
}
Also, by convention, capitalized function names are used for so called constructors:
var comment = new Comment();
That is a function operator (as opposed to a function statement or function constructor)
精彩评论