나는 당신이 당신의 예를 어딘가에 새로운 애니메이션 (동일 뭔가)을 설정하는 의미 생각합니다. 새로운을 사용하지 않고 내가 무슨 일에 약간의 정교한 것이다 :
var animate = function(){ console.log(0, 'animate'); };
animate.angular = function(){ console.log(1, 'animate.angular'); };
animate.circular = function(){ console.log(2, 'animate.circular'); };
animate.prototype.angular = function(){ console.log(3, 'animate.prototype.angular'); };
animate.prototype.circular = function(){ console.log(4, 'animate.prototype.circular'); };
첫 번째 두 가지 기능 # 1 & # 2의 애니메이션 변수에서 호출 할 수 있습니다.
animate.angular();
animate.circular();
당신이 새로운 애니메이션()를 작성하는 경우 당신이 다음 두, # 3 & # 4, 호출 할 수 있습니다 (하지만 # 1 또는 # 2).
var ani2 = new animate();
ani2.angular();
ani2.circular();
또한 animate()는 함수이지만 ani2는 그렇지 않습니다.
console.log(5, typeof animate);
console.log(6, typeof ani2);
console.log(7, animate());
ani2는 이미 생성되었지만 animate.prototype을 통해 새 구성원을 추가 할 수 있습니다.
animate.prototype.bark = function(){ console.log(8, 'bark'); };
ani2.bark();
animate 변수는 프로토 타입 형식을 상속받지 않습니다. 회원을 상속하지 않습니다 ani2가 애니메이션 변수에 직접 적용하는 것이
는
console.log(9, typeof ani2.bark);
console.log(10, typeof animate.bark);
참고. animate.prototype에서만 상속을받습니다.
animate.paperclip = function(){ console.log(11, "paperclip"); };
animate.paperclip();
console.log(12, typeof ani2.paperclip);
console.log(13, typeof animate.paperclip);
또한 새로운 아이들에게 인스턴스 멤버를 추가하는 애니메이션과 같은 생성자 함수 내부하여 이 키워드를 사용할 수 있습니다. 이 수정 된 때문
var Anime = function(a,b){ this.a=a; this.b=b; this.c=console; };
var anime1 = new Anime(14, 'anime1');
var anime2 = new Anime(15, 'anime2');
anime1.c.log(anime1.a, anime1.b);
anime2.c.log(anime2.a, anime2.b);
Anime.prototype.a = 16;
Anime.prototype.z = 'z';
var anime3 = new Anime(17, 'anime3');
anime3.c.log(18, anime3.a, anime3.b, anime3.z, " ", anime2.a, anime2.b, anime2.z, " ", anime1.a, anime1.b, anime1.z);
anime2.z='N';
anime3.c.log(19, anime3.a, anime3.b, anime3.z, " ", anime2.a, anime2.b, anime2.z, " ", anime1.a, anime1.b, anime1.z);
메모리가 자동으로 anime2.z의 별도의 인스턴스에 할당 된, anime1 & anime3 여전히 '공유'알뜰 수정되지 않은 Z.
a, b 및 c 멤버는 같은 방식으로 "공동"이 아닙니다. 그들은 즉시 생성자에 new Anime() (Anime.prototype에서 상속받지 않음)을 사용하여 할당되었습니다. 또한 프로토 타입의 멤버는 항상 생성자에 의해 "개별화"됩니다.
새로운 키워드를 잊지 마십시오. 그렇지 않으면 아무 것도 작동하지 않습니다. 예를 들어, 이은 없이 호출 된 생성자의 전역 개체를 가리 킵니다.
console.log(20, typeof window.a, typeof window.b, typeof window.c);
var opps = Anime(21, 'zapp');
console.log(22, typeof window.a, typeof window.b, typeof window.c);
console.log(23, typeof opps);
다음은 출력 결과입니다. 그리고 Tom이 Douglas Crockford 비디오를 제안하는 데는 두 번째입니다!
/*
1 animate.angular
2 animate.circular
0 animate
3 animate.prototype.angular
4 animate.prototype.circular
5 function
6 object
0 animate
7 undefined
8 bark
9 function
10 undefined
11 paperclip
12 undefined
13 function
14 anime1
15 anime2
18 17 anime3 z 15 anime2 z 14 anime1 z
19 17 anime3 z 15 anime2 N 14 anime1 z
20 undefined undefined undefined
22 number string object
23 undefined
*/
프로토 타입 상속 (특히 wrt JavaScript)에 대해 설명하는 또 다른 멋진 대답은 다음과 같습니다. http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript/1598077 # 1598077 –
관련 링크 : http://stackoverflow.com/questions/186244/what-does-it-mean-that-javascript-is-a-prototype-based-language –