내부에 기능을 포함하고 있는 객체
1-1. 함수 선언식 : 익명함수
function (){
구현 로직
};
1-2. 함수 선언식 : 기명함수
function name(){
구현 로직
};
2. 함수 표현식
const newFunc = function(){
구현 로직
};함수에 전달될 인수들, 배열 형태의 객체. 유사 배열이기 때문에 배열 메소드를 포함하지 않는다.
function name(a, b, c){
구현 로직
};
name(arg1, arg2, arg3); 함수안에서 유효범위를 갖는다.
let a = 1;
function outer(){
console.log(a); //위의 a 참조 1
function inner(){
console.log(a); //호이스팅 발생 var a; > undefined
var a = 3;
}
inner(); // undefined
console.log(a); //같은 유효범위에 있는 바깥 a 참조 1
}
outer(); // 1, undefined, 1
console.log(a); //1
> 1
> undefined
> 1
> 1array.in
new 키워드로 객체를 생성하는 함수이고, 이름이 대문자로 시작하는 것을 규칙으로 한다.
function Student(name){ // ---- "new 키워드를 호출하여 객체를 생성하는" >> 생성자 함수를 정의한다.
this.name = name; // ---- this는 생성자 함수를 가르킨다.
}
const student = new Student('Yuri'); // ---- new 키워드로 "생성자 함수 호출"하여 student 객체를 생성한다.
console.log(student.name);
> Yuri객체를 위한 공간을 따로 만들고 this 키워드가 해당 공간(인스턴스 객체)을 참조하도록 한다. 사용자 정의 객체 타입 또는 내장 객체 타입의 인스턴스를 생성한다.
function Person(name, age){
this.name = name;
this.age = age;
}
var foo = new Person('Lee', 29);객체를 생성하는 방법 (http://victorydntmd.tistory.com/51)
1.객체 리터럴 - 가장 일반적인 {중괄호}를 사용하는 법
var person = {
name: "yuri",
email: "bonethecomer@gmail.com",
birth: "0425",
}2.생성자 함수 - new 키워드를 이용하여 객체 생성자 함수를 호출하여 "빈 객체를 얻는 방식"
var person = new Object(); //기존 함수가 존재하지 않기 때문에 생성자 함수를 호출하는게 아니라 빈 객체를 생성하는 함수를 호출함
console.log(person.name); //undefined
person.name = "yuri"; //person에 키, 밸류로써 속성/property 값을 넣는 방식
console.log(person.name);
> yuri3.생성자 함수 - "기존 함수"에 new 연산자를 붙여서 호출하면 해당 함수는 생성자 함수로 동작
function Person(name, email){
this.name = name;
this.email = email;
}
var person1 = new Person("yuri", "bonethecomer@gmail.com");
var person2 = new Person("lucky", "lucky@gmail.com");
console.log(person1.name + ", " + person1.email);
console.log(person2.name + ", " + person2.email);
> yuri, bonethecomer@gmail.com
> lucky, lucky@gmail.com