const a = 1;
const b = 2;
const result = a + b;
document.write(a + b);
위 식을 함수로 바꾸면 밑의 식이 됩니다.
function add (a, b) {
document.write(a + b);
}
function add (a, b) {
document.write(a + b);
}
add(1, 2);
3
함수를 호출하고 인수를 부여하면 인수가 인자로 들어가 함수에 접근하게 된다.
function fruits() {
return 'banana';
return 'apple';
return 'grape'
}
document.write(fruits());
banana
인자는 함수 정의에 사용되는 변수를 말하고 인수는 함수의 호출에 사용되는 변수를 말한다.
function 함수(aaa, bbb){
return aaa+bbb;
}
document.write(함수(3,4));
let fruits = ['banana', 'apple', 'grape'];
banana | apple | grape |
---|---|---|
0 | 1 | 2 |
let fruits = ['banana', 'apple', 'grape'];
document.write (fruits[0] + '<br/>');
document.write (fruits[1] + '<br/>');
document.write (fruits[2]+ '<br/>');
banana
apple
grape
function fruits(){
return ['banana', 'apple', 'grape'];
}
let fruit = fruits();
document.write(fruit[0] + '<br/>');
document.write(fruit[1] + '<br/>');
document.write(fruit[2]+ '<br/>');
banana
apple
grape
function fruits(){
return ['banana', 'apple', 'grape'];
}
let fruit = fruits();
for(let i = 0; i < fruit.length; i++){
document.write(fruit[i].toUpperCase()+'<br/>');
}
BANANA
APPLE
GRAPE
let num = [1, 2, 3, 4, 5]
document.write(num.length);
5
let num = [1, 2, 3, 4, 5]
num.push(6);
document.write(num);
1,2,3,4,5,6
let num = [1, 2, 3, 4, 5]
num = num.concat(6, 7);
document.write(num);
1,2,3,4,5,6,7
let num = [1, 2, 3, 4, 5]
num.unshift(6);
document.write(num);
6,1,2,3,4,5
let num = [1, 2, 3, 4, 5]
num.splice(1, 1, 6)
document.write(num)
1,6,3,4,5
let num = [1, 2, 3, 4, 5];
num.shift();
document.write(num)
2,3,4,5
let num = [1, 2, 3, 4, 5];
num.pop();
document.write(num);
1,2,3,4
let num = [3, 2, 1, 4, 5]
num.sort();
document.write(num);
1,2,3,4,5
자바스크립트는 객체(object) 기반의 스크립트 언어이며 자바스크립트를 이루고 있는 거의 “모든 것”이 객체이다. 원시 타입(Primitives)을 제외한 나머지 값들(함수, 배열, 정규표현식 등)은 모두 객체이다.
자바스크립트의 객체는 키(key)과 값(value)으로 구성된 프로퍼티(Property)들의 집합이다. 프로퍼티의 값으로 자바스크립트에서 사용할 수 있는 모든 값을 사용할 수 있다.
객체는 데이터를 의미하는 프로퍼티(property)와 데이터를 참조하고 조작할 수 있는 동작(behavior)을 의미하는 메소드(method)로 구성된 집합이다.
배열과 객체는 유사하다. 하지만 객체는 원소의 index값에 문자나 숫자 등 원하는 데이터를 지정할 수 있다.
let grades = {'김철수': 80, '이영희': 90, '최혜림': 100};
'김철수': 80
, '이영희': 90
, '최혜림': 100
'김철수'
, '이영희'
, '최혜림'
80
, 90
, 100
vat grades = {};
grades['김철수'] = 80;
grades['이영희'] = 90;
grades['최혜림'] = 100;
let grades = {'김철수': 80, '이영희': 90, '최혜림': 100};
document.write(grades['최혜림']+'<br/>');
document.write(grades.최혜림+'<br/>');
100
100
let grades = {'김철수': 80, '이영희': 90, '최혜림': 100};
for(key in grades) {
document.write(key + '<br/>');
}
김철수
이영희
최혜림
let grades = {'김철수': 80, '이영희': 90, '최혜림': 100};
for(key in grades) {
document.write(grades[key] + '<br/>');
}
80
90
100
let grades = {'김철수': 80, '이영희': 90, '최혜림': 100};
for(key in grades) {
document.write('<li>'+grades[key]+'</li>');
}
80 90 100
프로퍼티 값이 함수일 경우, 일반 함수와 구분하기 위해 메소드라 부른다. 즉, 메소드는 객체에 제한되어 있는 함수를 의미한다. 메소드를 수행하기 위해서는 객체를 통해서 해당 메소드를 수행하여야 한다. 즉 그 동작을 수행하는 주체는 객체이며 그 동작을 수행하기 위해서는 객체에게 그 동작을 수행하라고 지시해야 한다.
let greet = {
'greeting' : function() {
document.write('Hello, World!');
}
}
greet['greeting']();
let greet = {
'greeting' : function() {
document.write(this);
}
}
greet['greeting']();
let greet = {
'list' : {'김철수': 80, '이영희': 90, '최혜림': 100},
'greeting' : function() {
console.log(this.list);
}
}
greet['greeting']();
[object Object] {
김철수: 80,
이영희: 90,
최혜림: 100
}
<참고>