Skip to content

Instantly share code, notes, and snippets.

@ide-an
Created April 27, 2013 10:21
Show Gist options
  • Save ide-an/5472614 to your computer and use it in GitHub Desktop.
Save ide-an/5472614 to your computer and use it in GitHub Desktop.
// Foo.prototype.f = ...でメソッドを追加するスタイル
var Foo = function(){};
Foo.prototype.f = function(){
console.log("foo");
};
Foo.prototype.g = function(){
console.log("bar");
};
// Bar.prototype = { ... } でメソッドを追加するスタイルだと...
var Bar = function(){};
//あらかじめ追加したフィールド(メソッド)fが...
Bar.prototype.f = function(){
console.log("foo");
};
//うっかり消されてしまう可能性がある
Bar.prototype = {
g: function(){
console.log("bar");
}
};
var bar = new Bar();
bar.g();//OK
bar.f();//error!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment