次のコード スニペット1を実行して、JS コンソールで何が起こっているかを確認してください。
私の質問は、スニペットの最後の行に関するものです:
- なぜ
F.prototype.method;
変更されるのですか? Fcustom.prototype.method
変更しないためには、どのように再定義すればよいF.prototype.method
ですか?
注: jQuery とアンダースコアを使用して関数を拡張しています。
1テスト コード スニペット:
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?