diff --git a/rules/S3498/javascript/rule.adoc b/rules/S3498/javascript/rule.adoc index 3e283f14a15..a2777f215b0 100644 --- a/rules/S3498/javascript/rule.adoc +++ b/rules/S3498/javascript/rule.adoc @@ -1,11 +1,12 @@ == Why is this an issue? -When an already-defined variable is given the same name within a new object, object-shorthand syntax is preferred as being more compact. Similarly, object-shorthand is also preferred for the definition of functions in object literals. +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. -=== Noncompliant code example +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] +[source,javascript,diff-id=1,diff-type=noncompliant] ---- let a = 1; @@ -17,10 +18,9 @@ let myObj = { } ---- +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. -=== Compliant solution - -[source,javascript] +[source,javascript,diff-id=1,diff-type=compliant] ---- let a = 1; @@ -32,7 +32,12 @@ let myObj = { } ---- +== Resources +=== Documentation +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer[MDN - Object initializer] +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions[MDN - Property definitions] +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#method_definitions[MDN - Method definitions] ifdef::env-github,rspecator-view[]