ES6 code programming style (1)

tags: javascript  es6

1. Block -level scope

1.let and const replaced VAR

The LET declaration variable will not be hung in Window, and will not cause global pollution; let new add -level agglomeration scope; let is not allowed to repeat declarations; let does not have a statement improvement, but it is improved to a temporary dead area.

CONST is the same as Let, but Const defines constants, not allowed to be modified, not allowed to change the memory address, and the Const declaration and assignment must be completed at one time.

console.log(a);
var a = 10;

console.log(b);
let b = 10;

The above code A will output UNDEFINED, B will directly report an error.

In the global environment, the use of CONST, especially in the global environment, should not be set up variables, but only constants should be set. Const declares that there are two benefits. One is that people who read the code immediately realize that this value should not be modified, and the other is to prevent errors caused by inadvertently modifying the value value. All functions should be set to constant.

2. String

Static string uses single quotes or anti -quotes, and dynamic strings use anti -attract numbers.

// bad
const a = "foobar";
const b = 'foo' + a + 'bar';

// acceptable
const c = `foobar`;

// good
const a = 'foobar';
const b = `foo${a}bar`;

3. Array

When using array members to assign variables, the deconstruction assignment is preferred.

const arr = [1, 2, 3, 4];

// bad
const first = arr[0];
const second = arr[1];

// good
const [first, second] = arr;

Use the extension operator (...) to copy and conversion to the array

The main role of the extended operator, expand the array, collect the array content

const arrCopy = [...arr];
 let arr = [...document.querySelectorAll('div')]
 console.log(arr)

Intelligent Recommendation

ES6 Functional Programming Notes-1

Good book Master some basics of ES6 to read The book progresses from shallow to deep Code warehouse Some concepts and codes First-class citizen When a gate allows the function to be used as any other ...

ES6 asynchronous programming (1)-Promise

Hey, hello everyone, let’s talk to you in this article today ES6 Implementation of asynchronous programming. of course,ES6 A variety of solutions are proposed in, let’s talk...

1 code style and habits

Java:  ...

[Java/code style] Programming style specification

   On gitbook a few days ago, I turned to Google’s [Java Code Programming Style Specification](https://legacy.gitbook.com/book/jervyshi/google-java-styleguide-zh/ details), I spent som...

Programming language | R code style

​ Just like writing, writing code is also code style. However, the code style is different from the style of writing. A person's style of writing can be arbitrary, and the code style is best to be &qu...

More Recommendation

simple rxjava code programming style

simple rxjava code programming style in app's gradle,need ref:...

JavaScript programming style, code specifications

Also wrote quite a long time js code, go back to the basic question many have not noticed, again seriously study the code specifications, or necessary to learn, a good start is half the battle, even i...

Cut Figure's Self-cultivation - [ES6] Programming Style Specification

Foreword No rules, no standards useletReplace var to define the variable. If it is a constant, useconst Static stringUniform useapostrophe'' , Dynamically stitched into a stringUniform useBack quote``...

2017.11.2 LeetCode - 70. Climbing Stairs - 100. Same Tree - 717. 1-bit and 2-bit Characters

717. 1-bit and 2-bit Characters We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Now given a string ...

Branch control structure in Java

20. Sequence structure The most basic structure of Java is the sequential structure, unless otherwise specified, it is executed in order, sentence by sentence Sequence structure is the simplest algori...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top