기존 방식 


function sum(a,b) {
   var c = a + b;
   return c;
}

sum(1,2); // 3

 


var sum = function (a,b) {
   var c = a + b;
   return c;
}

sum(1,2); // 3

ES6 화살표 함수

function 생략(삭제) (a,b) {  사이에 화살표를 삽입 (a,b) => {


let sum = (a,b) => {
   let c = a + b;
   return c;
};

sum(1,2); // 3

참 쉽죠 ^^

객체 함수(?) 뭔가 복잡해 보이지만 있어 보이는 함수

const  calc = { 
  sum: (a,b) => { 
    return a+b;
  }, 
  sub: (a,b) => {
    return a-b;
  } 
};

calc.sum(1,2); // 3

calc.sub(4,1); // 3

 

w3scholos.com

https://www.w3schools.com/js/js_arrow_function.asp

Posted by 비니미니파

object 유무 체크 #1

undefined

console.log( typeof objName );  // 결과 undefined

// objName 체크
if ( typeof objName != "undefined" )  {

}


체크 #2

console.log( document.getElementById("objName") );  // 결과 null 

// objName 체크 
if ( document.getElementById("objName")  != null ) { 

}

 

Posted by 비니미니파

Javascript 로 Excel round 비슷하게 구현 ( 엑셀과 똑같진 않다. )

Excel round 와 비슷하게 자리수 지정 한 만큼 반올림 해 준다.


var roundXL = function (num, digits) {
   digits = Math.pow(10, digits);
   return Math.round(num * digits) / digits;
};

// 예 가 잘못 되어 있었네요 수정 합니다.

roundXL(123456, 2);

roundXL(123456, -2);

결과 ( 2자리 기준으로 반올림 )

123500

roundXL(1234.1234 , 2);

결과 ( 소수점 2자리 기준으로 반올림 )

1234.12

Posted by 비니미니파