prototype屬性允許您向Boolean()對(duì)象添加屬性和方法。
注意:prototype是一個(gè)全局屬性,幾乎所有對(duì)象(數(shù)字,數(shù)組,字符串和日期等)都可用。
Boolean.prototype.name = value
創(chuàng)建一個(gè)新的布爾方法來更改段落的背景:
Boolean.prototype.isEven = function() {
if (this.valueOf() == true) {
return "lightgreen";
} else {
return "red";
}
};然后創(chuàng)建一個(gè)布爾值,并調(diào)用isEven()方法:
function myFunc() {
var a = true;
document.getElementById('result').style.background = a.isEven();
}所有瀏覽器完全支持prototype屬性:
| 屬性 | ![]() | ![]() | ![]() | ![]() | ![]() |
| prototype | 是 | 是 | 是 | 是 | 是 |