배열도 객체의 한 종류이다.
객체 - 속성 / 메서드 : 호출의 주체가 명확한 예
method와 function는 비교가 되야됩니다.
method는 호출의 주체가 명확하기 때문에 나중에 this를 명시할 수 있다.
function 함수는 호출의 주체가 없다.
obj1.test() --> method
testFunc() -> function
사람
속성(명사) : 눈 코 입 머리
메서드(동사) : 팔을 휘두른다. 말을한다.
let person = {
name: "wonjang",
eyeCnt: 2,
isHavingMouth: true,
팔을휘두르다: function () {
console.log("아파요");
},
};
length라는 속성은 배열의 최대 인덱스 +1 을 의미할 뿐이다.
다른 언어에서 배열은 요소가 모두 동일한 데이터 타입을 가져야함
let testArr = [];
testArr[200] = 3;
console.log(testArr.length);
let testArr = [
3,
"sam",
function () {
return "sam";
},
[2, 3, 4],
];
console.log(testArr);
큐 : 먼저들어온놈이 먼저 나가는 것
스택 : 먼저들어온놈이 나중에 나가는 것 / 나중에 온놈이 나가는것 push,pop
let testArr = [];
testArr.push(3);
console.log(testArr);
testArr.push("sam");
console.log(testArr);
testArr.pop();
// pop 은 배열에 가장 나중에 들어온 녀석을 뺀다
console.log(testArr);
큐를 구현해보자 shift- 맨 앞에게 빠짐, unshift-맨 앞으로 들어감
let people = ["다영", "성민", "준호", "휘인"];
// console.log(people);
people.shift();
// console.log(people);
people.unshift("다영2");
console.log(people);
shift와 unshift는 맨 앞에 있는 값을 빼야하기 때문에, 원래 있던 값을 밀어줘야한다. 그래서 더 시간이 걸린다
반복문, 배열은 반복문에 최적화되어있음
let testArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < testArr.length; i++) {
if (testArr[i] % 2 == 0) {
console.log(testArr[i]);
}
}
배열 더 공부하기 for in / for of , 배열에서는 for of를써라
배열을 for in 으로 하게되면, 인덱스값이 나온다.console
let testArr = [4,3,5,8];
for (idx in testArr) {
console.log(testArr[idx]);
} 인덱스는 0번째,1번째 .. 위치라고 보면됨. 4는 testArr의 0번째임. 이것을 인덱스라고함
for (value of testArr) {
console.log(value);
}
for of로 하게되면, 바로 value(값)을 알 수 있다. 그래서 배열에는 for of를 쓰는 것이다.