English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
prototype属性可以让你的属性和方法添加到Array()对象。
注意:prototype是一个全局属性,几乎所有对象(数字,布尔值,字符串和日期等)都可用。
Array.prototype.name = value
Dieser Beispiel erstellt eine neue Array-Methode, die die Array-Werte in Großbuchstaben umwandelt:}}
Array.prototype.upper = function() { for (var i = 0; i < this.length; i++) { this[i] = this[i].toUpperCase(); } };
Dann erstellen Sie ein Array und rufen Sie die upper()-Methode auf:
var fruits = ['Banana', 'Mango', 'Apple']; fruits.upper();
Alle Browser unterstützen das prototype-Attribut vollständig:
Eigenschaft | |||||
prototype | Ja | Ja | Ja | Ja | Ja |
Nachfolgender Beispiel verwendet das prototype-Attribut, um Eigenschaften zum fruits-Objekt hinzuzufügen:
Array.prototype.creator = 'ME';Testen Sie heraus‹/›