ERLEDIGT
NEIN
NEIN
ANTWORTEN
4
4
ZUGRIFFE
289
289
EMPFEHLEN
-
Ich versuche folgendes:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Cool = { hallo : function() { alert('hallo'); }, cu : function() { alert('see you!'); } } Eins = { x : 'foo' } Eins.prototype = Cool; Eins.hallo();
Aber es funkioniert nicht.
Wie kann ich die Vererbung realisieren, ohne jede einzelne Methode zuzuweisen?TypeError: Eins.hallo is not a function
Gruß
DataFox
-
Code javascript:
1 2 3 4 5 6 7 8 9 10 11 12
Cool = function(){ this.hallo= function() { alert('hallo'); } this.cu = function() { alert('see you!'); } } Eins=new Cool; Eins.x='foo'; Eins.hallo();
-
14.11.08 23:39 #3
- Registriert seit
- Oct 2004
- Ort
- Leipzig
- Beiträge
- 589
Mhm das verwundert mich nun da Object eigentlich die Methode Object.prototype besitzt in der Regel.
Denn Date , Array etc leiten sich alle von Object ab. Aber es kann möglich sein das was Du dort hast bereits ein Objekt ist. Also in der Objekt Notation und es somit keine Konstruktor Funktion gibt.
Mit einen simplen Trick geht es dennoch , angenommen wir schreiben uns ne Funktion aber das ist mehr dazu da um Objekte zu erweitern und macht der Entwickler von dem Prototype Framework so. Wenn mich einer fragt nicht die optimalste Lösung aber kann man doch ab und zu gut gebrauchen um JSON Objekte zu erweitern.
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Object.extend = function (object,source) { for(property in source) { object[property] = source[property]; } }; var object1 = { name:"test", getName:function(){ return this.name; } } var object2 = { alter:10, getAlter:function(){ return this.alter; } } Object.extend(object2,object1); // nun sollte wenn alles gut gelaufen ist // object2 alles von Object 1 übernommen haben for(key in object2) alert(key);
-
Oder noch ne andere Variante:
Code javascript:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } Cool = { hallo : function() { alert('hallo'); }, cu : function() { alert('see you!'); } } Eins = Object.create(Cool); Eins.x ='foo'; Eins.hallo(); Zwei = Object.create(Eins); alert(Zwei.x);
Hier gefunden: http://javascript.crockford.com/prototypal.html
Und wenn ein "JS-Papst" darauf schwört, ist das fast soetwas wie das 11.Gebot
-
05.02.10 23:10 #5
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
hier noch ne kleine Erweiterung zur obigen Variante um den Aufruf einer Methode der "Basis-Klasse":
Code javascript:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>JS Inheritance</title> </head> <body> <script type="text/javascript"> if(typeof Object.create !== 'function'){ Object.create = function(base){ function Class() {}; Class.prototype = base; return new Class(); }; } var base = new function(){ var value = "base"; this.getValue = function(){ return value; } }; alert(base.getValue()); var derived = Object.create(base); derived.getValue = function(){ return this.__proto__.getValue() + "->" + "derived"; }; alert(derived.getValue()); var derived2 = Object.create(derived); derived2.getValue = function(){ return this.__proto__.getValue() + "->" + "derived2"; }; alert(derived2.getValue()); </script> </body> </html>
//Edit sorry für das herauskramen des alten Threads...
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
C++ - Vererbung
Von chronixTT im Forum C/C++Antworten: 4Letzter Beitrag: 08.01.11, 11:25 -
Vererbung...
Von SonMiko im Forum Flash PlattformAntworten: 3Letzter Beitrag: 09.03.08, 11:27 -
Vererbung
Von hagbard_celine im Forum CSSAntworten: 1Letzter Beitrag: 17.12.07, 03:27 -
<div> Vererbung
Von Chris B im Forum Javascript & AjaxAntworten: 3Letzter Beitrag: 26.10.05, 09:50 -
Vererbung ?
Von Speedkill im Forum .NET ArchivAntworten: 2Letzter Beitrag: 30.05.05, 14:01





Zitieren

Login





