Using Moo 1.2 has taught me a lot about JavaScript. Of course, that means I've made a lot of mistakes but that seems to be the best way for me to learn. While browsing the Moo source, I'd always wondered the difference between arrays using brackets ([]) and braces ({}). Here's what I learned.
使用Moo 1.2已使我了解了很多有关JavaScript的知识。 当然,这意味着我犯了很多错误,但这似乎是我学习的最佳方法。 浏览Moo源代码时,我总是想知道使用方括号([])和大括号({})的数组之间的区别。 这是我学到的。
Use brackets for an array of simple values.
使用方括号表示简单值数组。
//examples var answers = ['yes','no','maybe']; var names = ['David','Kristina','Charlie','Angela'];Use braces for key => value arrays and objects/properties.
将大括号用于键=>值数组和对象/属性。
//example - random array var programmer = { 'name':'David Walsh', 'url':'https://davidwalsh.name', 'girl':'Kristina'} //example - used for an object's properties var Element.implement({ getText: function(){ return this.get('text'); } });This is similar to PHP's array system.
这类似于PHP的数组系统。
$arr = array('name'=>'David','position'=>'Programmer');Have anything to add? Please share!
有什么要补充的吗? 请分享!
翻译自: https://davidwalsh.name/javascript-arrays-brackets-braces