uruchom następujący fragment kodu i zobaczyć, co dzieje się w konsoli JS:Jak ponownie zdefiniować metodę w „javascript klasy”
Moje pytania są odnośnie ostatniej linii w urywka:
- Dlaczego zmieniono
F.prototype.method;
? - Jak mogę zmienić definicję
Fcustom.prototype.method
, aby nie zmieniaćF.prototype.method
?
Uwaga: używam jQuery i podkreślenia rozszerzenie funkcji.
kod testowy fragment:
var F = function() {}; F.prototype.method = function() { // some code } F.prototype.method; // it shows "some code" Fcustom = $.extend(true, F, {}); _.extend(Fcustom.prototype, { method: function() { // other code } }); Fcustom.prototype.method; // it shows "other code" F.prototype.method; // it shows "other code" instead of "some code" Why?
Czy chcesz sklonować funkcję 'F' do' Fcustom'? – pimvdb
Niestety nie można klonować funkcji ... –