티스토리 뷰
// 객체 정의 시작
var Person = function(name, age){
this.name = name;
this.age = age;
};
Person.prototype.say = function(){//prototype영역, 공유영역
console.log(this.name);
};
//객체 정의 종료
var jin = new Person('jin', '19');//인스턴스(객체) 생성
var jong = new Person('jong', '24');
// jin.say();
// jong.say();
// console.dir(jin);// constructor is Person
var literalObj = {};// 인스턴스(객체) 생성
// console.dir(literalObj ); //constructor is Object
// literalObj.prototype.say = function(){
// console.log('literalObj');
// } ==>TypeError: literalObj.prototype is undefined
console.dir(literalObj.prototype);//undefined
// 알 수 있는 점
1. 객체 리터럴로 객체생성 방법으로는 prototype영역 정의를 할 수 없다.
2. prototype영역을 사용하려면 객체 생성 전에 정의를 미리 해야한다.
--> 생성된 객체(인스턴스)가 공유하는 메소드들을 정의하기 위해서는 prototype영역을 사용해야한다.
'javascript' 카테고리의 다른 글
scroll이 생긴 요소의 현재 스크롤위치를 알 수 있는 방법 (0) | 2017.02.04 |
---|---|
javascritp 상속 (0) | 2016.12.21 |
함수호출 방식에 따른 this의 바인딩에 대해서...[출처:https://blog.outsider.ne.kr/439] (0) | 2016.09.16 |
[JavaScript] 브라우저 크기 구하기 : 화면 해상도 구하기 (0) | 2016.09.12 |
How to get selected tab index when clicking - jqueryUI - tabs (0) | 2016.08.22 |