Object

  • one of the JavaScriptโ€™s data types.
  • a collection of related date and/or functionality.
  • Nearly all objects in JavaScript are instances of Object.

function print(name,age) {
  console.log(name);
  console.log(age);
}

ํ•จ์ˆ˜์˜ parameter์— ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ, ์ธ์ž๊ฐ€ ๋งŽ์•„์ง€๋ฉด ์ถ”๊ฐ€ํ•ด์•ผํ•˜๋Š” ๊ฒƒ๋“ค์ด ๋งŽ์•„์ง„๋‹ค.
๋กœ์ง€์ปฌํ•˜๊ฒŒ ๋ฌถ์–ด์„œ ์ƒ๊ฐํ•  ์ˆ˜๋„ ์—†์Œ!

์ด๊ฑธ ๊ฐœ์„ ํ•˜๊ณ ์ž ์“ฐ๋Š” ๊ฒƒ์ด Object โญ๏ธ


function print(person) {
  console.log(person.name);
  console.log(person.age);
}
const ellie = { name: 'ellie', age: 4 };
print(ellie);

  1. Literals and Properties

const obj1 = {}; // 'object literal' syntax
const obj2 = new Object(); // 'object constructor' syntax

new command๋ฅผ ๋„ฃ์–ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด, Object์—์„œ ์ •์˜๋œ constructor๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค!


const ellie = { name: 'ellie', age: 4 };
print(ellie);

ellie.hasJob = true;
console.log(ellie.hasJob); // true
delete ellie.hasJob;
console.log(ellie.hasJob); // undefined

JS๋Š” ํƒ€์ž…์ด runtime(ํ”„๋กœ๊ทธ๋žจ์ด ๋™์ž‘ํ•˜๊ณ  ์žˆ์„ ๋•Œ)์— ๊ฒฐ์ •๋˜๋Š” ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•จ.

  1. Computed properties

์ •ํ™•ํ•˜๊ฒŒ ์–ด๋–ค key๊ฐ€ ํ•„์š”ํ•œ์ง€ ๋ชจ๋ฅผ๋•Œ, ์ฆ‰ runTime์—์„œ ๊ฒฐ์ •๋  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•
์ฝ”๋”ฉํ•  ๋•Œ์—๋Š” . ๋ฌธ๋ฒ•์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒŒ ๋งž์Œ!

console.log(ellie.name); // ellie
console.log(ellie['name']); // ellie
ellie['hasJob'] = true;
console.log(ellie['hasJob']); // true

object ์•ˆ์—์žˆ๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ string ํ˜•ํƒœ๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค

  • object์˜ property๋ฅผ . ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ๊ณ 
  • ๋˜๋Š” computed properties ๋ฌธ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋“ฏ์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค

๋‹จ, computed properties๋กœ ๋ฐ›์•„์˜ฌ ๋•Œ key๋Š” ํ•ญ์ƒ stringํƒ€์ž…์„ ์ง€์ •ํ•ด์„œ ๋ฐ›์•„์™€์•ผํ•œ๋‹ค!


  1. Property value shorthand

key์™€ value์˜ ์ด๋ฆ„์ด ๋™์ผํ•˜๋‹ค๋ฉด ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.


const Person1 = { name : 'bob', age: 2 };
const Person2 = { name : 'steve', age: 3 };
const Person3 = { name : 'dave', age: 4 };

const Person4 = Person('ellie',30);
console.log(person4);
 
function Person(name, age) {
  return {
    name,
    age
  }
}

Person1,2,3์ฒ˜๋Ÿผ ๋™์ผํ•œ key์™€ value๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋˜ ๊ฒƒ์„
=> ํšจ์œจ์ ์œผ๋กœ ๊ฐ’๋งŒ ๋„ฃ์œผ๋ฉด ์ถœ๋ ฅ๋˜๊ฒŒ ๋ณ€๊ฒฝ.

class template๊ณผ ๋™์ผํ•œ ๊ตฌ์กฐ. class๊ฐ€ ์—†์—ˆ์„ ๋•Œ๋Š” ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ๋‹ค.

  1. Constructor function
function Person(name, age) {
  // this = {};
  this.name = name,
  this.age = age
  // return this;
}
const Person4 = new Person('ellie',30);

์ด๋ ‡๊ฒŒ ๋‹ค๋ฅธ ๊ณ„์‚ฐ์„ ํ•˜์ง€ ์•Š๊ณ , ์ˆœ์ˆ˜ํ•˜๊ฒŒ object๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋“ค์€ ๋ณดํ†ต ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
return๋„ ํ•˜์ง€ ์•Š๊ณ  ์ด๋ ‡๊ฒŒ class์ฒ˜๋Ÿผ this๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
ํ˜ธ์ถœ ์‹œ์—๋„ class์—์„œ ์ƒˆ๋กœ์šด object๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, new๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. in operator : property existance check (key in obj)

: ๊ฐ„๋‹จํ•˜๊ฒŒ key์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค์›Œ๋“œ, ์žˆ๋‹ค๋ฉด true, ์—†๋‹ค๋ฉด false

console.log('name' in ellie); // true
console.log('age' in ellie); // true
console.log('random' in ellie); // false
console.log(ellie.random); // undefined
  1. forโ€ฆin vs forโ€ฆof
  • for (key in obj)
for (key in ellie) {
  console.log(key); 
}

ellie๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š” key๋“ค์ด, ๋ธ”๋Ÿญ์„ ๋Œ ๋•Œ๋งˆ๋‹ค key๋ผ๋Š” ์ง€์—ญ๋ณ€์ˆ˜์— ํ• ๋‹น์ด ๋œ๋‹ค!

๋ชจ๋“  ํ‚ค๋“ค์„ ๋ฐ›์•„์™€์„œ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ for in ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

  • for (value of iterable)
    ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ๋ฆฌ์ŠคํŠธ, ์ˆœ์ฐจ์ ์œผ๋กœ Iterableํ•œ ๊ฒƒ๋“ค์„ ์“ด๋‹ค.

const array = [1,2,3,4,5];
for(let i = 0; i < array.lenth; i++) {
  console.log(value);
}

์ด๊ฒƒ ๋ณด๋‹ค๋Š” ํ›จ์”ฌ ๊ฐ„๋‹จํ•œ for of


for(value of array) {
  console.log(value);
}

  1. Fun Cloning
    Object.assign(dest, [obj1,obj2,obj3โ€ฆ])

const user = {name: 'ellie', age:'20'};
const user2 = user;

  • ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•?
  1. old way
const user3 = {};
for(let i = 0; i < user.length; i++) {
  user3[key] = user[key];
}
console.log(user3);


  1. Object.assign

// const user4 = {};
// user4 = Object.assign(user); ๋นˆ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ž˜์ฒ˜๋Ÿผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

const user4 = Object.assign({ }, user );
console.log( user4 );


  • ๋‹ค๋ฅธ ์˜ˆ์‹œ

const fruit1 = {color : 'red'};
const fruit2 = {color : 'blue', size:'big'};
const mixed = Object.assign({},fruit1, fruit2);

console.log(mixed.color); // blue
console.log(mixed.size); // big

์•ž์— ๋™์ผํ•œ property๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ฐ’์ด ๊ณ„์† ๋ฎ์–ด์”Œ์›Œ์ง„๋‹ค

Array์™€ API

  • array์™€ object์˜ ์ฐจ์ด์ !



  1. Declaration

const arr1 = new Array();
const arr2 = [1,2];
  1. Index position
const fruits = ['๐Ÿ“','๐ŸŒ'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]); //๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐœ์ฒด๋ฅผ ์ฐพ์„ ๋•Œ
console.log(fruits[1]);
console.log(fruits[2]); //undefined
console.log(fruits[fruits.length - 1]); //๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐœ์ฒด๋ฅผ ์ฐพ์„ ๋•Œ

  1. Looping over an array

a. for loop

  for(let i = 0; i < fruits.length; i ++){
    console.log(fruits[i]);
  }

b. for of loop

for (fruit in fruits) {
  console.log(fruits[fruit])
}

c. for Each loop


fruits.forEach(function(fruit, index, array) {
  console.log(fruit,index,array);
});
// anonymous function์€ arrow function์œผ๋กœ ์ถ•์•ฝ ๊ฐ€๋Šฅ
fruits.forEach((fruit, index) => {
  console.log(fruit,index);
});


  • Performs the specified action for each element in an array. array์— ๋“ค์–ด์žˆ๋Š” ๊ฐ๊ฐ์˜ ์—˜๋ฆฌ๋ฉ˜ํŠธ์— ์ •ํ•ด์ง„ ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜

๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’ ๋งˆ๋‹ค ์šฐ๋ฆฌ๊ฐ€ ์ „๋‹ฌํ•œ ์•ก์…˜=์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค

  1. Addition, deletion, copy

a. push : add an item to the end ๋์— ์ถ”๊ฐ€

fruits.push('๐Ÿ“','๐Ÿ‹');

b. pop : remove an item from the end ๋งจ ๋์˜ ์•„์ดํ…œ์„ ์‚ญ์ œ

fruits.pop();
fruits.pop();

c. unshift : add an item to the beginning

fruits.unshift('๐Ÿ“','๐Ÿ‹'); // ๋ฐฐ์—ด์˜ ์•ž์— ์•„์ดํ…œ ์ถ”๊ฐ€

d. shift : remove an item from the beginning

fruits.shift(); // ๋ฐฐ์—ด์˜ ์•ž์—์„œ๋ถ€ํ„ฐ ์•„์ดํ…œ์„ ์‚ญ์ œ

โญ๏ธ ์ค‘์š” โญ๏ธ
shift, unshift are slower than pop, push!



e. splice : remove an item by index position splice(index, count)

fruits.push('๐Ÿ“','๐Ÿ‘','๐Ÿ‹');
console.log(fruits);
fruits.splice(1,1); // 1๋ฒˆ ์ธ๋ฑ์Šค์—์„œ 1๊ฐœ๋งŒ.
fruits.splice(1); //์›ํ•˜๋Š” ์ธ๋ฑ์Šค๋งŒ ์ง€์ •ํ•˜๊ณ  ๊ฐฏ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ทธ ์ธ๋ฑ์Šค ๋’ค์˜ ์•„์ดํ…œ์„ ์‹น ์ง€์šด๋‹ค
console.log(fruits);

fruits.splice(1,1,'๐Ÿ','๐Ÿ‰');
// ์ง€์šฐ๊ณ  ๋‚˜์„œ ๊ทธ์ž๋ฆฌ์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
fruits.splice(1,0,'๐Ÿ','๐Ÿ‰');
// ์ง€์šฐ์ง€ ์•Š๊ณ ! ๊ทธ ์ž๋ฆฌ ๋‹ค์Œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

f. concat combine two arrays ์ƒˆ๋กœ ๋ฌถ์—ฌ์ง„ ๋ฐฐ์—ด์ด ํ•ฉ์ณ์ ธ์„œ ์ƒˆ๋กœ ๋ฆฌํ„ด๋จ.

const fruits2 = ['๐Ÿ','๐Ÿฅ‘'];
const newFruits = fruits.concat(fruits2);

console.log(newFruits);
  1. Searching

a. indexOf : find the index

console.log(fruits);
console.log(fruits.indexOf('๐ŸŽ')); // 0
console.log(fruits.indexOf('๐Ÿ‰')); // 3
console.log(fruits.indexOf('๐Ÿฅ')); // -1 ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅ

b. includes

console.log(fruits.includes('๐Ÿ‰')); //true
console.log(fruits.includes('๐Ÿฅ')); //false

c. lastIndexOf lastIndexOf : ๋’ค์—์„œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ indexOf : ์•ž์—์„œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰

fruits.push('๐ŸŽ');
console.log(fruits); //['๐ŸŽ','๐Ÿ','๐Ÿ‰','๐Ÿ‘','๐Ÿ‹','๐ŸŽ'];

console.log(fruits.indexOf('๐ŸŽ')); // 0
console.log(fruits.lastIndexOf('๐ŸŽ')); // 5