English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
prototype属性は、Boolean()オブジェクトに属性とメソッドを追加するのに許可されています。
注意:prototypeはグローバル属性であり、ほぼすべてのオブジェクト(数字、配列、文字列、日付など)で使用できます。
Boolean.prototype.name = value
新しいブールメソッドを作成して段落の背景を変更する:
Boolean.prototype.isEven = function() { if (this.valueOf() == true) { return "lightgreen"; } else { return "red"; } };
次に、ブール値を作成し、isEven()メソッドを呼び出します:
function myFunc() { var a = true; document.getElementById('result').style.background = a.isEven(); }
すべてのブラウザが完全にprototype属性をサポートしています:
属性 | |||||
prototype | はい | はい | はい | はい | はい |