以下の内容はhttps://shuzo-kino.hateblo.jp/entry/2016/05/08/153018より取得しました。


prototypeを使うと既存オブジェクトを拡張可能

prototypeは既存オブジェクトに手を加えるメソッドです。

実際のところ

オブジェクト指向的な書き方をした、以下の様な関数があったとします。

function Person(fist, last) {
 this.first = fist;
 this.last = last;
}

p = new Person('gogo', 'yubari')

このままだと、当然メソッドはありません

p.fullName()
// undefined

ここで、prototype経由でメソッドを設定してから読むと……ちゃんと動きます。

Person.prototype.fullName = function() { return this.first + " " + this.last }

console.log(p.fullName())
//=> "gogo yubari"

更に……なんと既存のオブジェクトに対する上書き拡張すら可能となっています。
わぁ何かRubyみたいだぁ……

String.prototype.toUpperCase() = function() { return null }

"hoge".toUpperCase()
// => null



以上の内容はhttps://shuzo-kino.hateblo.jp/entry/2016/05/08/153018より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14