Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 20, 2022 02:17 am GMT

50 hints JS(ES6) developer must know (3rd part)

Today, We are going to have some more tips in usage of JS.
String
Use single quotes '' for strings.

// badconst name = "Capt. Janeway";// bad - template literals should contain interpolation or newlinesconst name = `Capt. Janeway`;// goodconst name = 'Capt. Janeway';

Strings that cause the line to go over 100 characters should not be written across multiple lines using string concatenation.

// badconst errorMessage = 'This is a super long error that was thrown because \of Batman. When you stop to think about how Batman had anything to do \with this, you would get nowhere \fast.';// badconst errorMessage = 'This is a super long error that was thrown because ' +  'of Batman. When you stop to think about how Batman had anything to do ' +  'with this, you would get nowhere fast.';// goodconst errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';

When programmatically building up strings, use template strings instead of concatenation.

// badfunction sayHi(name) {  return 'How are you, ' + name + '?';}// badfunction sayHi(name) {  return ['How are you, ', name, '?'].join();}// badfunction sayHi(name) {  return `How are you, ${ name }?`;}// goodfunction sayHi(name) {  return `How are you, ${name}?`;}

Do not unnecessarily escape characters in strings.

// badconst foo = '\'this\' \i\s \"quoted\"';// goodconst foo = '\'this\' is "quoted"';const foo = `my name is '${name}'`;

Functions
Use named function expressions instead of function declarations.

// badfunction foo() {  // ...}// badconst foo = function () {  // ...};// good// lexical name distinguished from the variable-referenced invocation(s)const short = function longUniqueMoreDescriptiveLexicalFoo() {  // ...};

Wrap immediately invoked function expressions in parentheses.

// immediately-invoked function expression (IIFE)(function () {  console.log('Welcome to the Internet. Please follow me.');}());

Note: ECMA-262 defines a block as a list of statements. A function declaration is not a statement.

// badif (currentUser) {  function test() {    console.log('Nope.');  }}// goodlet test;if (currentUser) {  test = () => {    console.log('Yup.');  };}

Never name a parameter arguments. This will take precedence over the arguments object that is given to every function scope.

// badfunction foo(name, options, arguments) {  // ...}// goodfunction foo(name, options, args) {  // ...}

Never use arguments, opt to use rest syntax ... instead.

// badfunction concatenateAll() {  const args = Array.prototype.slice.call(arguments);  return args.join('');}// goodfunction concatenateAll(...args) {  return args.join('');}

Use default parameter syntax rather than mutating function arguments.

// really badfunction handleThings(opts) {  // No! We shouldnt mutate function arguments.  // Double bad: if opts is falsy it'll be set to an object which may  // be what you want but it can introduce subtle bugs.  opts = opts || {};  // ...}// still badfunction handleThings(opts) {  if (opts === void 0) {    opts = {};  }  // ...}// goodfunction handleThings(opts = {}) {  // ...}

Avoid side effects with default parameters.

var b = 1;// badfunction count(a = b++) {  console.log(a);}count();  // 1count();  // 2count(3); // 3count();  // 3

Always put default parameters last.

// badfunction handleThings(opts = {}, name) {  // ...}// goodfunction handleThings(name, opts = {}) {  // ...}

Never use the Function constructor to create a new function.

// badvar add = new Function('a', 'b', 'return a + b');// still badvar subtract = Function('a', 'b', 'return a - b');

Spacing in a function signature.

// badconst f = function(){};const g = function (){};const h = function() {};// goodconst x = function () {};const y = function a() {};

Never mutate parameters.

// badfunction f1(obj) {  obj.key = 1;}// goodfunction f2(obj) {  const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;}

Never reassign parameters.

// badfunction f1(a) {  a = 1;  // ...}function f2(a) {  if (!a) { a = 1; }  // ...}// goodfunction f3(a) {  const b = a || 1;  // ...}function f4(a = 1) {  // ...}

Prefer the use of the spread syntax ... to call variadic functions.

// badconst x = [1, 2, 3, 4, 5];console.log.apply(console, x);// goodconst x = [1, 2, 3, 4, 5];console.log(...x);// badnew (Function.prototype.bind.apply(Date, [null, 2016, 8, 5]));// goodnew Date(...[2016, 8, 5]);

Functions with multiline signatures, or invocations, should be indented just like every other multiline list in this guide: with each item on a line by itself, with a trailing comma on the last item.

// badfunction foo(bar,             baz,             quux) {  // ...}// goodfunction foo(  bar,  baz,  quux,) {  // ...}// badconsole.log(foo,  bar,  baz);// goodconsole.log(  foo,  bar,  baz,);

Thanks for your time.
I hope this blog will give you strong inspirations for your development.
Next time, I will deploy about the Arrow Functions in JS.


Original Link: https://dev.to/stormytalent/50-hints-jses6-developer-must-know-3rd-part-5enk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To