2009-10-27 3 views

답변

12

이것은 자바 스크립트의 일부이며 jquery에만 국한되지 않습니다.

prototype 속성은 해당 유형의 모든 개체에서 공유하는 메서드와 속성을 정의합니다.

MyClass

function MyClass() 
{ 
} 

myClass.prototype.myMethod = function() 
{ 
    alert("hello world"); 
} 

var myObject = new MyClass(); 
myObject.myMethod(); 

모든 인스턴스는 (주) 방법 myMethod()있을 것이다.

프로토 타입의 메서드는 생성자 내에서 선언 된 메서드와 동일한 가시성을 갖지 않습니다.

예를 들어

:

function Dog(name, color) 
{ 
    this.name = name; 

    this.getColor = function() 
    { 
     return color; 
    } 
} 

Dog.prototype.alertName = function { 
    alert(this.name); 
} 

Dog.prototype.alertColor = function { 

    //alert(color); //fails. can't see color. 
    //alert(this.color); //fails. this.color was never defined 

    alert(this.getColor()); //succeeds 
} 

var fluffy = new Dog("Fluffy","brown"); 
관련 문제