73 lines
2.1 KiB
Plaintext
73 lines
2.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In JavaScript, object shorthand syntax is a more concise way to define properties on objects. It was introduced to make object literals more readable and expressive.
|
|
|
|
In the shorthand syntax, if a variable exists in the scope with the same name as the object key you're defining, you can omit the key-value pair and just write the variable name. The interpreter will automatically understand that the key and the variable are linked.
|
|
|
|
Using object shorthand syntax can make your code cleaner and easier to read. It can also reduce the chance of making errors, as you don't have to repeat yourself by writing the variable name twice.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
let a = 1;
|
|
|
|
let myObj = {
|
|
a : a, // Noncompliant
|
|
fun: function () { // Noncompliant
|
|
//...
|
|
}
|
|
}
|
|
----
|
|
|
|
You can omit the property name and the colon if it is the same as the local variable name. Similarly, you can omit the `function` keyword for method definitions.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let a = 1;
|
|
|
|
let myObj = {
|
|
a,
|
|
fun () {
|
|
//...
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer[Object initializer]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions[Property definitions]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#method_definitions[Method definitions]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use shorthand for [property|method] "xxx".
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Property name
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 19 Jan 2016, 18:25:07 Elena Vilchik wrote:
|
|
\[~ann.campbell.2] I don't understand highlighting: RHS? what do you mean?
|
|
|
|
=== on 20 Jan 2016, 08:56:14 Ann Campbell wrote:
|
|
Sorry [~elena.vilchik]. I thought that was a more common shorthand that it apparently is.
|
|
|
|
RHS = right-hand side
|
|
|
|
LHS = left-hand side
|
|
|
|
endif::env-github,rspecator-view[]
|