Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsuru793/2ed5dee301ce56c5c51d to your computer and use it in GitHub Desktop.
Save mitsuru793/2ed5dee301ce56c5c51d to your computer and use it in GitHub Desktop.
JavaScriptのクラスとインスタンスのprototypeは同じなのか?
// console.logのエイリアス
var l = function(x) { console.log(x); };
prototypeOfConstructor = null
function People(name) {
this.name = name;
prototypeOfConstructor = this.prototype;
}
// Peopleを定義しただけで、中身が実行されていないことを確認
l(prototypeOfConstructor); // null
suzuki = new People("suzuki");
l(prototypeOfConstructor === People.prototype); // false
l(prototypeOfConstructor === suzuki.prototype); // true
l(People.prototype === suzuki.prototype); // false
// 上記から下記のことが分かりました。
// * prototypeOfConstructorは、インスタンス変数のprototypeである。
// * クラスとインスタンスのprototypeは別物
// このクラスとはfuncitonオブジェクトのことを指しています。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment