ES란, ECMAScript의 약자이며 자바스크립트의 표준, 규격을 나타내는 용어이다.
새로운 언어 기능이 포함된 주요 업데이트
업데이트 리스트
const and let
- const는 변경되지 않으며 재할당 불가. 새로운 값을 제공하려고 하면 오류가 반환됩니다.
- let은 새로운 값을 가질 수도 있고 재할당 가능
Arrow functions(화살표 함수)
만약 함수의 본문(body)에 return만 있는 경우 화살표 함수는 return과 {}를 생략할 수 있다. 단, 같이 생략해야한다.
return문에서 소괄호는 사용가능하다.
// 함수 표현식 - 화살표 함수 (생략형)
const plusFn = (a, b) => a + b;
let str3 = `Hello ${str1} ${str2}`;
const myFunc = (name, age = 22) => {
return `안녕 ${name} 너의 나이는 ${age}살 이니?`;
};
console.log(myFunc1('영희'));
// 출력 => 안녕 영희 너의 나이는 22살 이니?
const contacts = {
famillyName: '이',
name: '영희',
age: 22
};
// ES5 문법
let famillyName = contacts.famillyName;
let name = contacts.name;
let myAge = contacts.age;
// ES6 문법
let { famillyName, name, age } = contacts;
// ./detailComponent In ES6
export default function detail(name, age) {
return `안녕 ${name}, 너의 나이는 ${age}살 이다!`;
}
// Other File
import detail from './detailComponent';
console.log(detail('영희', 20));
// 출력 => 안녕 영희, 너의 나이는 20살 이다!
const myPromise = () => {
//resolve및 reject로 예상 오류를 처리 할 수 있습니다.
return new Promise((resolve, reject) => {
resolve('안녕하세요 Promise가 성공적으로 실행했습니다');
});
};
cosole.log(myPromise());
// Promise {<resolved>: "안녕하세요 Promise가 성공적으로 실행했습니다"}
function fun1(...theArgs) {
console.log(theArgs.length);
}
fun1(); // 0
fun1(5); // 1
fun1(5, 6, 7); // 3
//출처: https://beomy.tistory.com/16 [beomy]
const arr = ['영희', 20, '열성적인 자바스크립트', '안녕', '지수', '어떻게 지내니?'];
const Func = (...anArray) => {
return anArray;
};
console.log(Func(arr));
// 출력 => ["영희", 20, "열성적인 자바스크립트", "안녕", "지수", "어떻게 지내니?"]
class myClass {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`안녕 ${this.name} 너의 나이는 ${this.age}살이다`);
}
}
// myClass 메서드 및 속성 상속
class UserProfile extends myClass {
userName() {
console.log(this.name);
}
}
const profile = new UserProfile('영희', 22);
profile.sayHello(); // 안녕 영희 너의 나이는 22살이다.
profile.userName(); // 영희
const = val
let = var
//선언
const travel_spot = new Array("1","2","3");
const travel_spot = [];
const travel_spot = new Array();
//접근
travel_spot[0];
//추가 및 삭제
travel_spot.push("4");
travel_spot.unshift("5");
travel_spot.splice(2,0,"6","7"); //2번째 인덱스에서 0개의 원소를 지우고 6 ,7 추가
travel_spot.pop();
travel_spot.shift();
//const pooh = ['pooh','bear','disney character', 'boy'];
const pooh = {
name : 'pooh',
species : 'bear',
job : 'disney character',
gender : 'boy',
sayHi : function(){
console.log("Say Hello");
}
}
pooh['say-bye'] = function(){
console.log("Say Bye");
}
//객체내 함수를 실행할 땐 ()붙이기
pooh.favorites = ['honey','friends','cake'];
console.log(pooh['say-bye']());
//객체 키값 삭제
delete pooh.favorites
//객체를 생성자로 만들기
function Character(name, species, job, gender){
this.name = name;
this.species = species;
this.job = job;
this.gender = gender;
this['sayHi'] = function(){
console.log('Say Hi2 Im ' + this.name);
};
this['say-bye'] = function(){
console.log("Say Hi2 ${this.gender}");
};
}
const winnie_the_pooh = new Character("winnie the pooh", 'bear', 'disney character', 'boy');
const snoopy = new Character("snoopy", 'dog', 'comic character', 'boy');
const pikachu = new Character("pikachu", 'squirrel', 'pokekmon character', 'boy');
//console.log(winnie_the_pooh['sayHi']());
console.log(pikachu['sayHi']());
//일반적인 함수 선언
function multiply10(num){
return num*10;
}
//arrow function은 여러가지를 생략해서 사용가능
//인자나 리턴값이 하나일 때 예시
const multiply10 = num => num*10;
const data = multiply10(1000);
console.log(data);
let age = 15;
//조건문
if (age > 19){
console.log("술을 마실 수 있다.");
}
else{
console.log("아직 이르다.");
}
// 삼항연산자 : 조건 ? 참일때 실행될 코드 : 거짓일때 실행될 코드
const result = age > 19 ? "술을 마실 수 있다." : "아직 이르다.";
console.log(result);
if (age < 10){
console.log("유아");
}else if(age <20){
console.log("10대");
}else if(age <30){
console.log("20대");
}else{
console.log("노인");
}
switch(age){
case 70:
console.log("70대");
break;
case 20:
console.log("20대");
break;
default:
console.log("응애");
}
const numArr = [77, 81, 12, 34, 51, 20];
for (let i=0; i<numArr.length ; i++){
console.log("for : "+ (parseInt(numArr[i])+10));
}
let i = 0;
while (i< numArr.length){
console.log("while : "+ (parseInt(numArr[i])+10));
i++;
}
//한번은 실행하고 싶으면 do while문
do{
console.log(i);
i++;
}while(i<11)
TypeScript가 무엇인지??