私はMozillaのthis articleで既にjsの継承(this one、this one、this oneなど)javascript継承の正しいプロトタイプの影響は何ですか?
上のいくつかの記事を読んで、 "古典" の継承は、このように示されている:(私は例を均一化)
// inherit Base
function Derived() { ... }
Derived.prototype = new Base(); <-------
Derived.prototype.constructor = Derived; <-------
しかしthis articleに私が参照してください。私もこれを見てきました
また// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype); <-------
Derived.prototype.constructor = Derived;
:
Derived.prototype = Base.prototype;
そして私はまた、実験とconstructor
気取りの使用を見つけることができませんでした:私はこの行をスキップした場合
Derived.prototype.constructor = Derived; <--- it still work if I skip this line
は、new Derived()
は正しくとにかくDerived()
を呼び出します。
だから、1)正しいものです:
Derived.prototype = new Base();
Derived.prototype = Object.create(Base.prototype);
Derived.prototype = Base.prototype;
- 他の?
2)は実際にはDerived.prototype.constructor = Derived;
ですか?どうして ?
それはjavascriptについてのことですが、あなたが好きなことをすることができます..私はジョンResigのclass.jsあなたのためにプロトタイプのナンセンスをラップするのが好きなので、あなたのコードでそれを見る必要はありません.http://ejohn.org/blog/simple-javascript-inheritance/ – andygoestohollywood
'Object.create()は比較的以前に存在しなかったコマンドです。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create(ブラウザの互換性の概要は、そのページの一番下にあります)を参照してください。それを持っているブラウザだけを使用する場合は、それを使用してください。それ以外の方法はありません。 :) –