closure
이처럼 자신을 포함하고 있는 외부함수보다 내부함수가 더 오래 유지되는 경우, 외부 함수 밖에서 내부함수가 호출되더라도 외부함수의 지역 변수에 접근할 수 있는데 이러한 함수를 클로저(Closure)라고 부른다.
scope chain 지역변수 찾고 전역변수 찾아감
var : js 전역으로 사용
let : block scope 그 안에서만 사용
const : 상수, 수정할 수 없음은 아님, 배열과 오브젝트 값 변경은 가능하다, 값 재할당만 안됨
const를 기본으로 사용한다.
변경될 수 있는 변수는 let을 사용한다.
var는 사용하지 않는다.
immutable array
뒤로가기 앞으로가기같은거에서 원래 데이터 기억하기 좋음
=======================
es6 string에 새로운 메서드들
startWith
endsWith
includes
=========================
Array
array에서 for in 안쓰는게 좋음 Array.prototype.getIndex 같은거 쓰면 object에 추가되기 쨰문
for(let value of data){} => 이게 더 좋음, 배열, string 등등
===============================
spread operator(...) : 펼침연산자
let pre = [10,50,100]
let newData = [...pre];
let newData = [1,2,3,...pre,7,8];
... : 기존의 참조를 끊고 새로운 복사
function sum(a,b,c){
return a+b+c)
sum.apply(null,pre) <-원래 사용방법
sum(...pre) <-펼쳐서 넣어줌
배열을 바꾸지 않고 복사하는 방법, 배열 합치거나 펼쳐진 상태로 함수에 파라미터로 전달할 때 사용
from method ============= 진짜 배열 만들기
function addMark(){
let newData = []
for(let i=0;i<arguments.length;i++){
newData.push(arguments[i]+"!")
}
}
function addMark(){
let nreArray = Array.from(arguments) //arguments를 배열로 만들어줌
let newData = arguments.map(function(value{
return value + "!"
})
}
addMark(1,2,3,4,5)
arguments는 입력안해도 받아줌
Destructuring= ===================
let data = ["crong","honux","jk","jinny"]
let jisu = data[0]
let jung= data[2]
let [jisu,,jung] = data
set ========================
중복없이 유일한 값을 저장하려고 할때, 이미 존재하는지 체크할 떄 유용
weakset
참조를 가지고 있는 객체만 저장이 가능하다
Array -> set, weakset
object -> map, weakmap
Arrow funtion ===================
funtion(){}
()=>{}
function(value){
return value*2
}
(value)=>(value*2)
//default parameter
function sum(value,sum=1){
return value*size
}
//rest parameter
개수가 변하는 argument를 배열로 처리할때
매개변수에 ...이 들어가면 배열로 받는다
function checkNum(...arg){
const result = arg.every((v)=>typeof v ==="number")
}
const result = checkNum(10,20,30,"55")
class =============================
모습만 class 이지 function임
class Health{
constructor(name,lastTime){
this.name = name;
this.lastTime = lastTime
}
}
object =======================
Object.create()
Object.assign()
Object.setPrototypeOf()
모든 객체는 object로 만들어져 있음
prototype.object
prototype.link -> __proto__ 체인형식으로 되있다
prototype
toString.call()
forEach(function(){}) -> foreach는 중간에 break, return을 할 수 없다.
nodelist, array 차이
Optional Chaining
'개발 > Javascript' 카테고리의 다른 글
체크박스 전체 선택 코드 (0) | 2023.06.18 |
---|---|
html에서 style, script 위치 (0) | 2022.01.18 |
데이터 타입 (0) | 2021.08.28 |
변수 (0) | 2021.08.28 |
javascript 란? (0) | 2021.08.28 |