admin管理员组文章数量:1389887
<script>
(function() {
$('html').addClass('js');
var contactForm = {
container: $('#contact'), <-- THIS COMMA
init: function() {
$('<button></button>', {
text: 'Contact Me'
})
.insertAfter('article:first')
.on('click', this.show);
}, <---------------------------------- AND THIS COMMA
show: function() {
contactForm.container.show();
}
};
contactForm.init();
})();
</script>
In the above script, I noticed:
container: $('#contact'),
Is that one way to declare a variable? Doing the following breaks the script:
var container = $('#contact');
Also, what is with the mas after the init function and the container variable (if it is a variable)?
<script>
(function() {
$('html').addClass('js');
var contactForm = {
container: $('#contact'), <-- THIS COMMA
init: function() {
$('<button></button>', {
text: 'Contact Me'
})
.insertAfter('article:first')
.on('click', this.show);
}, <---------------------------------- AND THIS COMMA
show: function() {
contactForm.container.show();
}
};
contactForm.init();
})();
</script>
In the above script, I noticed:
container: $('#contact'),
Is that one way to declare a variable? Doing the following breaks the script:
var container = $('#contact');
Also, what is with the mas after the init function and the container variable (if it is a variable)?
Share Improve this question edited May 30, 2012 at 20:22 Frédéric Hamidi 263k42 gold badges496 silver badges485 bronze badges asked May 30, 2012 at 20:21 eveoeveo 2,85315 gold badges65 silver badges99 bronze badges 1- 4 This is Javascript object syntax and is not specific to jQuery. – Frédéric Hamidi Commented May 30, 2012 at 20:23
5 Answers
Reset to default 8This way you declare an object:
var contactForm = {
// properties:
property1 : value,
property2 : value,
property3 : value,
// methods:
method1 : function() {
// ...
},
method2 : function() {
// ...
}
};
You can find more information about JavaScript objects in MDN ...and in the ments below :)
That block of code (beginning with var contactForm = {
) is declaring an object using object literal notation. Commas separate the different key-value pairs in the object literal notation.
var obj = { key1: value1, key2: value2 };
Commas are used to separate name/value pairs when defining objects:
var newObj = {
name: value,
another_name: another_value
};
That is called Object literal notation.
Which is basically a Comma-Separated list of name/value pairs, wrapped in curly braces.
Advantage of this notation is that all data within the braces are encapsulated and not defined globally which avoids conflicts with other scripts or libraries.
Those are key:value
pairs of the object contactForm
you defined separated by mans
var obj = { key1 : value1,
key2 : value2
method1 : function(){
// definition
},
method2 : function(){
// definition
}
}
The thing that confused me in my early days is that what is function(){....}
doing inside a variable(in this case an object) definition until I discovered functions are objects too in js which may not be the same case in language you have used earlier .
and this function function(){....}
without name is called a anonymous function.
Here's a good thread on anonymous function Why do you need to invoke an anonymous function on the same line?
本文标签: Commas in JavascriptStack Overflow
版权声明:本文标题:Commas in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744653650a2617834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论