function func(...args) {
console.log(args);
}
+) arguments 객체란? 함수 내부에서 사용할 수 있는 배열 비슷한 객체.
: 배열이나 객체의 요소를 개별 요소로 펼치는 문법 (...)
let f3;
let x = 100;
let f1 = function () {
let x = 40;
f3 = function (x = 10) {
console.log(x);
}
}
f1();
f3();
var gx=1;
gy=2;
function test(){
var x = 20;
y = 30;
console.log(x, y,gx,gy);
console.log(window.x, window.y,
window.gx, window.gy);
console.log(global.x, global.y,
global.gx, global.gy);
}
test()
클로저 : 함수가 선언될 때 그 함수가 선언된 환경 (스코프) 를 기억
스코프와 변수의 수명
function outer() {
const outerVariable = "I am from outer";
function inner() {
console.log(outerVariable); // 외부 함수의 변수를 참조
}
return inner;
}
const closureFunction = outer(); // outer 실행, inner 반환
closureFunction(); // "I am from outer" 출력
핵심 특징
1. 외부 함수가 종료된 후에도 외부 함수의 변수를 기억
2. 변수 상태를 유지, 캡슐화에 활용
각각 실행할 때는 0, 1, 2
하지만 클로저가 들어갔을 때는 3, 3, 3을 출력 (이미 실행이 끝났기 때문)
var ar = [1, "hello", function(){return 3;}];
function print(f) {
f():
}
// IOC (Inversion Of Control)
// 함수의 인자로 전달
print(function(){
console.log("hello");
});
var nums = [4, 7, 3, 1, 6, 3, 2, 11, 9];
console.log(nums);
nums.sort(function(a, b){
return b - a;
});
console.log(nums);
let nums = [4,7,3,1,6,3,2,11,9];
console.log(nums);
var result = nums.filter(function(item){
return item > 4;
});
var result = nums.filter(function(item, index){
return index > 4;
});
let result = nums.filter(function(item, index, arr){
return item > arr[2];
});
console.log(result);
JavaScript Object Notation, 데이터를 저장하거나 전송할 때 많이 사용하는 경량의 데이터 교환 형식, 단순히 데이터를 표시하는 표현 방법
let nums = new Array();
let nums = new Array(1, 2, 3, 4, 5, 6, 7);
// JSON으로 작성하기
let nums = [];
let nums = [1, 2, 3, 4, 5, 6, 7];
let notice = new Object();
notice.id = 1;
notice.title = "hello json";
// JSON 으로 작성하기s
let notice = {"id":1, "title":"hello json"}
let notice = {id:1, title:"hello json"}