admin管理员组

文章数量:1355522

I've seen numerous threads about bracket vs dot notation when accessing objects....however I'm going through freeCodeCamp and when I'm accessing an object with a syntax such as SomeonesName['online'] it will suggest I should use dot notation instead?

Besides being quicker.....what exactly is the benefit? Everything I've seen minus "speed" seems to imply bracket notation is better?

Is there something I'm missing? perhaps another thing that the "dot" operator does better?

I've seen numerous threads about bracket vs dot notation when accessing objects....however I'm going through freeCodeCamp and when I'm accessing an object with a syntax such as SomeonesName['online'] it will suggest I should use dot notation instead?

Besides being quicker.....what exactly is the benefit? Everything I've seen minus "speed" seems to imply bracket notation is better?

Is there something I'm missing? perhaps another thing that the "dot" operator does better?

Share Improve this question edited Apr 21, 2017 at 2:08 user663031 asked Apr 21, 2017 at 1:59 msmith1114msmith1114 3,2599 gold badges49 silver badges105 bronze badges 2
  • What have you seen that implies bracket notation is better? Most style guides and/or standard lint rulesets encourage/remend/require dot notation where possible. – user663031 Commented Apr 21, 2017 at 2:08
  • I've seen some people use minifiers that shorten variables to reduce the size of their code files before publishing but keep ["strings"] unchanged, so they'd use brackets for public variables. – Domino Commented Apr 21, 2017 at 3:30
Add a ment  | 

5 Answers 5

Reset to default 4

The rule of thumb is very simple. Everytime you can use dot notation, use it. You only need to use bracket notation when you have to manipulate invalid JavaScript identifiers or when you need to pass a variable key. Consider this example:

var obj = {
  foo: "Foo",
  _bar: "Bar",
  $baz: "Baz",
  "foo-bar": "Foo Bar",
  quux: "Quux"
};
    
console.log(obj.foo);
console.log(obj._bar);
console.log(obj.$baz);
console.log(obj['foo-bar']);
    
var quux = "quux";
    
console.log(obj[quux]);


Values can be retrieved from an object by wrapping a string expression in a [ ] suffix. If the string expression is a string literal, and if it is a legal JavaScript name and not a reserved word, then the . notation can be used instead. The . notation is preferred because it is more pact and it reads better.

Douglas Crockford, JavaScript: The Good Parts

. Dot notation is faster to write and clearer to read. [] Square bracket notation allows access to properties containing special characters and selection of properties using variables.

Please go through for details: https://developer.mozilla/en/docs/Web/JavaScript/Reference/Operators/Property_accessors

The two following lines are functionally identical in javascript;

SomeonesName.online
SomeonesName["online"]

You only really need to use square brackets if you are accessing a key with a name that is invalid (such as one containing a . or -) or if you are using a variable.

Otherwise, the dot notation is nicer to read and write, and most linters and programmers prefer it.

In regards to your edit - they are both functionally identical. They take the same amount of time to run, and with the right IDE they can even take the same amount of time to write. It's purely a matter of preference. There is no benefit apart from style consistency.

. & [] both notations are functionally same and no difference in execution speed. The difference is only when the accessing key having some special characters which conflicts in javascript while accessing the property.

I would say use which ever one your more fortable with. Just be consistent. I used codeacademy, and sometimes your code will be correct, but freeCodeCamp my looking for a specific way for you to do it in order to move to the next lesson. hope this helps

本文标签: Javascript suggesting dot notationStack Overflow