프로토타입 메소드 선언하기
클래스 함수에는 프로퍼티만 선언
function Student(num,name,address) { this.num=num; this.name=name; this.address=address; }클래스 함수에 프로토타입 메소드 추가
=> 객체에 상관없이 메모리(Method 영역)에 하나만 생성되어 모든 객체가 공유하여 사용
Student.prototype.display=function() { alert("학번 = "+this.num+", 이름 = "+this.name+", 주소 = "+this.address); } Student.prototype.setValue=function(num,name,address) { this.num=num; this.name=name; this.address=address; }프로토타입 메소드의 효율적인 관리를 위해 Object 객체({})의 요소로 메소드 선언
// !클래스 함수에는 프로퍼티만 선언 Student.prototype={ "display":function() { alert("학번 = "+this.num+", 이름 = "+this.name+", 주소 = "+this.address); }, "setValue":function(num,name,address) { this.num=num; this.name=name; this.address=address; } }
alert(student instanceof Student);//true
alert("num" in student);//true
alert("display" in student);//true
alert("phone" in student);//false
for(variable in student) {
//alert(variable);
//요소의 이름을 제공받아 객체 요소를 사용하기 위해 . 연산자 대신 [] 연산자 이용
if(typeof(student[variable])!="function") {//객체의 요소가 메소드가 아닌 경우
alert("객체의 속성값 = "+student[variable]);
}
}
with(student) {
alert("학번 = "+num+", 이름 = "+name+", 주소 = "+address);
}