[ Web Proxy ]
URL:
Viewing: https://ko.javascript.info/coding-style [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
2022 8 4

.

. .

( ).

[]

, .

.

. , .

(Egyptian) . . .

if (condition) {
  //  1
  //  2
  // ... n...
}

if (condition) doSomething() . ?

.

  1. , .
    if (n < 0) {alert(`Power ${n} is not supported`);}
  2. , . .
    if (n < 0)
      alert(`Power ${n} is not supported`);
  3. .
    if (n < 0) alert(`Power ${n} is not supported`);
  4. .
    if (n < 0) {
      alert(`Power ${n} is not supported`);
    }

if (cond) return null . .

. .

:

// (`)        .
let str = `
  ECMA International's TC39 is a group of JavaScript developers,
  implementers, academics, and more, collaborating with the community
  to maintain and evolve the definition of JavaScript.
`;

if .

if (
  id === 123 &&
  moonPhase === 'Waning Gibbous' &&
  zodiacSign === 'Libra'
) {
  letTheSorceryBegin();
}

. 80 120 .

.

  • :

    (Tab) . , .

    .

    .

    show(parameters,
         aligned, //      
         one,
         after,
         another
      ) {
      // ...
    }
  • :

    . , , .

    function pow(x, n) {
      let result = 1;
      //              <--
      for (let i = 0; i < n; i++) {
        result *= x;
      }
      //              <--
      return result;
    }

    . .

.

. . .

StandardJS . .

.

continue .

if .

for (let i = 0; i < 10; i++) {
  if (cond) {
    ... // <-     .
  }
}

continue .

for (let i = 0; i < 10; i++) {
  if (!cond) continue;
  ...  // <-     .
}

if/else return .

.

1:

function pow(x, n) {
  if (n < 0) {
    alert("'n'    .");
  } else {
    let result = 1;

    for (let i = 0; i < n; i++) {
      result *= x;
    }

    return result;
  }
}

2:

function pow(x, n) {
  if (n < 0) {
    alert("'n'    .");
    return;
  }

  let result = 1;

  for (let i = 0; i < n; i++) {
    result *= x;
  }

  return result;
}

n < 0 ' , return . .

.

  1. //  
    function createElement() {
      ...
    }
    
    function setHandler(elem) {
      ...
    }
    
    function walkAround() {
      ...
    }
    
    //    
    let elem = createElement();
    setHandler(elem);
    walkAround();
  2. ,

    //    
    let elem = createElement();
    setHandler(elem);
    walkAround();
    
    // ---   ---
    function createElement() {
      ...
    }
    
    function setHandler(elem) {
      ...
    }
    
    function walkAround() {
      ...
    }
  3. :

.

' . .

' , , , .

, .

, .

:

. , .

Linter

Linter , .

, . ' linter linter .

linter:

linter . ESLint .

linter . .

ESLint linter .

  1. Node.js .
  2. npm( ) ESLint . npm install -g eslint
  3. ( ) .eslintrc .
  4. ESLint . ESLint .

.eslintrc .

{
  "extends": "eslint:recommended",
  "env": {
    "browser": true,
    "node": true,
    "es6": true
  },
  "rules": {
    "no-console": 0,
    "indent": ["warning", 2]
  }
}

"extends" "eslint:recommended" . .

. http://eslint.org/docs/user-guide/getting-started .

IDE lint ESLint .

.

" ?" " ?" . .

.

: 4

.

function pow(x,n)
{
  let result=1;
  for(let i=0;i<n;i++) {result*=x;}
  return result;
}

let x=prompt("x?",''), n=prompt("n?",'')
if (n<=0)
{
  alert(`Power ${n} is not supported, please enter an integer number greater than zero`);
}
else
{
  alert(pow(x,n))
}

.

.

function pow(x,n)  // <-    
{  // <-    
  let result=1;   // <-   = /  
  for(let i=0;i<n;i++) {result*=x;}   //   < /  
  // { ... }     
  return result;
}

let x=prompt("x?",''), n=prompt("n?",'') // <--    ,
//      ,  /    ;   
if (n<=0)  // <- (n <= 0)     ,   ( )
{   // <-    
  //            
  alert(`Power ${n} is not supported, please enter an integer number greater than zero`);
}
else // <- "} else {" else      
{
  alert(pow(x,n))  //  ;  
}

.

function pow(x, n) {
  let result = 1;

  for (let i = 0; i < n; i++) {
    result *= x;
  }

  return result;
}

let x = prompt("x?", "");
let n = prompt("n?", "");

if (n <= 0) {
  alert(`Power ${n} is not supported,
    please enter an integer number greater than zero`);
} else {
  alert( pow(x, n) );
}

.

Web Proxy Viewer  |  New URL  |  Original Page