Dokumentacja języka JavaScript 1.5:Obiekty:Function:prototype
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
[edytuj] Podsumowanie
Wartość instancji od której są tworzone poszczególne klasy. Każdy obiekt, który może zostać utworzony poprzez wywołanie konstruktora funkcji posiada wspólną własność prototypu funkcji.
| Własność obiektu: Function | |
| Zaimplementowana w: | JavaScript 1.1, NES 2.0 |
| Wersja ECMA: | ECMA-262 |
[edytuj] Opis
Możesz dodawać nową własność lub metodę do istniejących klas poprzez dodanie im prototypu funkcji powiązanego z konstruktorem funkcji dla tych klas. Składnia dla dodawania nowej własności lub metody jest następująca:
fun.prototype.name = value
gdzie
-
fun - Nazwa obiektu konstruktora funkcji który chcesz zmieniać.
-
name - Nazwa tworzonej własności lub metody.
-
value - Wartość wstępnie przypisana do nowej własności lub metody.
If you add a property to the prototype for an object, then all objects created with that object's constructor function will have that new property, even if the objects existed before you created the new property. For example, assume you have the following statements:
var array1 = new Array(); var array2 = new Array(3); Array.prototype.description=null; array1.description="Contains some stuff" array2.description="Contains other stuff"
After you set a property for the prototype, all subsequent objects created with Array will have the property:
anotherArray=new Array() anotherArray.description="Currently empty"
Note that prototype is itself an object, and can be assigned properties and methods via the object literal syntax:
function MyFunction() {
alert("Created.");
}
MyFunction.prototype = {
alert1: function(str) {
alert(str);
},
five: 5,
alert2: function() {
alert("Hi.");
}
};
var myObject = new MyFunction();
myObject.alert1("There.");
myObject.five;
myObject.alert2();
[edytuj] Przykłady
The following example creates a method, str_rep, and uses the statement String.prototype.rep = str_rep to add the method to all String objects. All objects created with new String() then have that method, even objects already created. The example then creates an alternate method and adds that to one of the String objects using the statement s1.rep = fake_rep. The str_rep method of the remaining String objects is not altered.
var s1 = new String("a")
var s2 = new String("b")
var s3 = new String("c")
// Create a repeat-string-N-times method for all String objects
function str_rep(n) {
var s = "", t = this.toString()
while (--n >= 0) s += t
return s
}
String.prototype.rep = str_rep
s1a=s1.rep(3) // zwraca "aaa"
s2a=s2.rep(5) // zwraca "bbbbb"
s3a=s3.rep(2) // zwraca "cc"
// Create an alternate method and assign it to only one String variable
function fake_rep(n) {
return "repeat " + this + " " + n + " times."
}
s1.rep = fake_rep
s1b=s1.rep(1) // zwraca "repeat a 1 times."
s2b=s2.rep(4) // zwraca "bbbb"
s3b=s3.rep(6) // zwraca "cccccc"
The function in this example also works on String objects not created with the String constructor. The following code returns "zzz".
"z".rep(3)