자바스크립트 기본 문법1

반응형
반응형

const , let

if (true) {
	 var x = 3;
 }
 console.log(x);
 
 if (true) {
	 const y = 3;
 }
 console.log(y); // 에러
const a = 0;
a = 1; // 에러

let b = 0;
b = 1; // 가능

const c; // 에러

 

템플릿 문자열

 

var num1 = 1;
var num2 = 2;
var result = 3;
var string1 = num1 + '더하기' + num2 + '는\' + result +'\';
console.log(string1);

=> ES5

const num3 = 1;
const num4 = 2;
const result2 = 3;
const string2 = `${num3} 더하기 ${num4} 는 ${result2}`;
console.log(string2);

 

객체 리터럴

var sayNode = function() { 
  console.log("Node");};

var es = 'ES';
var oldObject = {
    sayJs: function() {
      console.log('JS');
    }, sayNode:sayNode,};

oldObject[es+6] = 'Fantastic';
oldObject.sayNode();
oldObject.sayJs();
console.log(oldObject.ES6);

=>

const newObject = {sayJS() { console.log('JS');},
                   sayNode, [es+6]:'Fantastic',};
newObject.sayNode();
newObject.sayJS();
console.log(newObject.ES6);

 

속성명과 변수명이 동일하다면 한번만 사용하는것이 가능하다.(ES2015부터);

 

화살표 함수

funtion add1(x,y) {
  return x + y; 
  }
  
  const add2 = (x,y) => { return x + y;};
  const add3 = (x,y) => x + y;
  const add4 = (x+y) => (x+y);
  
  function not1(x) {
    return !x;
   }
   
   const not2 = x => !x;

 

var relationship1 = {name:'klom', 
                     friends:['nero','hero','xero'],
                     logFriends : function() { var that = this;

this.friends.forEach(function(friend) {
   console.log(that.name, friend); });
}, };

relationship1.logFriends();
const relationship2 = {
	name : 'zero', 
    friends: ['nero','hero','xero'],
	logFriends() {
      this.friends.forEach(friends => {console.log(this.name,friend); });
 },};
 
 relationship2.logFriends();

 

구조분해 할당 : 구조분해란 객체와 배열로 부터 속성이나 요소를 쉽게 꺼낼 수 있다.

var candyMachine = { status: {
                               name : 'node',
                               count : 5,
                             },
                     getCandy: function() {
                             this.status.count--;
                             return this.status.count;
                                           },
                    };
 var getCandy = candyMachine.getCandy;
 var count = candyMachine.status.count;

 

=> 

const candyNachine = { status: {
                               name : 'node',
                               count : 5,
                             },
                     getCandy: function() {
                             this.status.count--;
                             return this.status.count;
                                           },
                    };

const {getCandy,status:{count}} = candyMachine;

 

배열에 따른 구조 분해 할당 문법

var array = ['nodejs',{},10,true];
var node = array[0];
var obj = array[1];
var bool = array[3]
const array = ['nodejs',{},10,true];
const[node,obj,bool] = array;
반응형

'프로그래밍 언어 > 자바스크립트' 카테고리의 다른 글

자바스크립트 기본 문법2  (0) 2020.09.10

댓글

Designed by JB FACTORY