0
이의 우리가 는
$.Animal = function(options) {
this.defaults = { name : null }
this.options = $.extend(this.defaults, options);
}
$.Animal.prototype.saySomething = function() {
alert("I'm animal!");
}
가 지금은 고양이 개체를 만들고 싶습니다 객체 동물을 가정 해 봅시다 ("생성자"포함) 개체의 모든 방법을 상속하지만, 그 중 일부를 수정합니다.
$.Cat.prototype.saySomething = function() {
alert("I'm cat!");
}
어떻게 새 개체 고양이를 생성하고 saySomething() 메소드를 재정의 동물에서 상속 할 수 있습니다 ... 그것은 $ .Annimal 절대적으로 유사하지만, 방법 saySomething()이 하나의 모양을?
감사합니다.
$.Cat=$.Dog.constructor; //Set the constructor
$.Cat.constructor=$.Dog.constructor;
var Native=function(){}; //Copy the prototype object
Native.prototype=$.Dog.prototype;
$.Cat.prototype=new Native();
//Assign new method
$.Cat.prototype.saySomething = function() {
alert("I'm cat!");
}
은 어디에서이 방법에 대한 자세한 내용을보실 수 있습니다 : – Kirzilla
잘 기억한다면 어쨌든 개념은 당신이 첫 번째 객체의 생성자를 가져 와서 두 번째 객체에 할당 한 다음 첫 번째 객체의 프로토 타입 객체를 복사하여 두 번째 객체에 할당한다는 것입니다. . 나는이 프로토 타입 객체를 복사하기위한 (obj.prototype의 c) 루프와는 고전이 다르다고 생각하지 않지만 더 빠릅니다. – mck89
프로토 타입 개체는 복사해야합니다. $ .Dog.prototype = $. Cat.prototype과 같은 프로토 타입은 복사 할 수 없지만 $ .Dog.prototype에 대한 참조이므로 프로토 타입을 수정해야합니다. 개 고양이도 변경합니다. – mck89