let age=1;
switch(value){
case1: console.log(1)
case2: console.log(2)
case3: console.log(3)
default: console.log("d")
break;
}
1 2 3 d (break를 중간에 안 쓰면 )
for (const item of array){
console.log(item)
}
for (const index in array){
console.log(array[index])
} // 잘 안씀 키값을 꺼내올 때 씀
const obj={
color: "blue",
width:200,
height:200
}
for (const key in obj){
console.log(key)
} //color, width,height
type FunFunction = ()=>string;
type Example = {
(url:string, search?:string) : Promise<string>;
}
interface Example {
(url:string, search?:string) : Promise<string>;
}
function sum(a,b,...args){
let s=0;
for(let i=0; i<args.length; i++){
s=s+args[i]
return s;
}
arguments 유사배열을 배열로 바꾸려면 ? Array.from()
sum(10,20,30)
sum.call(null,10,20,30);
sum.apply(null,[10,20,30]);
const arr = [10,20,30];
sum.apply(null,arr);
(function(){console.log("dd")})()
function* gen(){
yield 10;
yield 20;
return 30;
}
const g =gen();
g.next();
g.next();
g.next();
async function example(){
}
const obj={
getData:function(){
return "data";
}
}
const obj={
getData(){
return "data";
}
}
const obj =Object.create(null,{
age:{
value:26,
writable:false, // 바꿀 수 없음
configurable: false // 지울 수 없음
}
})
function salePrice(discountRate){
return function(price){
return price-(prcie*(discountRate * 0.01))
}
}
console.log(salePrice(30)(200000))
//반환값으로 함수가 올때 아래에 방법 권장
let summerPrice=salePrice(30);
console.log(summerPrice(200000))
function timer(s:number):Promise<string>{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
if(Math.random()*10===2){
resolve("good luck");
}else{
reject("fail");}
},s)
})
}
timer(2000)
.then((result:string)=>{console.log(result)})
.catch((error:string)=>{console.log(error)})
async function hello(){
try{
const result =await timer(2000);
console.log(result);
}catch(e){
console.log(e);
}
}
hello();
let makeObj={
name:"lim",
age:20
}
function makeObj(name,age){
return {
name,age
}
}
makeObj("lim",20)
//함수가 구조 변경 시 용이함
class Person {
construnctor(name,age){
this.name =name;
this.age=age;
}
}
const makeObj = new Person("lim",20)
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
const box ={a:1, b:2};
const box2 =Object.assign({},box);
const box3 = {...box};
const box4 = JSON.parse(JSON.stringify(box));
https://ko.javascript.info/private-protected-properties-methods
() - Round brackets or parentheses
[] - Square brackets
{} - Curly brackets or braces
<> - Angle brackets or chevrons
원래 배열 수정 안 함 => slice
원래 배열 수정함 => splice
const objs = [
{a: "1"},
{b: "ss"},
{c: "yy"}
]
const reducedObj = objs.reduce((a,b)=>({...a,...b}),{})
//console.log(reducedObj)
// {
a:"1",
b:"ss",
c:"yy"
}
private 과 protected 외부로 공개x
차이점 private 클래스 자체 protected 상속받은 클래스도 ok
const array =[];
for(const item of items){
const row =[];
for(const [key,value] of Object.entries(item)){
row.push(value);
}
array.push(row.join());
}
console.log(array.join("&&&"))