Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 1.65 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function PaintObj(posX, posY, color)
  2. {
  3.         this.posX = posX;
  4.         this.posY = posY;
  5.         this.color = color;
  6. }
  7.  
  8. //Prototypfunktionen anlegen
  9. PaintObj.prototype.createElement = function()
  10. {
  11.         console.log("square -> createelement");         //Konsoleneintrag für Firebug
  12.        
  13.         var paintArea = document.getElementById("paintArea");
  14.        
  15.         this.div = document.createElement("div");
  16.         this.div.style.position = "absolute";
  17.         this.div.style.top = this.posY + "px";
  18.         this.div.style.left = this.posX + "px";
  19.         this.div.style.background = "#" + this.color;
  20.        
  21.         paintArea.appendChild(this.div);
  22.        
  23. }; //Strichpunkt weil: weil ich einer funktion direkt gleich etwas zuweise und nicht oben erstelle. Wenn ich die Funktion direkt oben im Konstruktor anlege dann bräuchte man keinen Strichpunkt.
  24.  
  25.  
  26.  
  27. PaintObj.prototype.deleteElement = function(div)
  28. {
  29.         var PaintArea = document.getElementById("paintArea");
  30.         paintArea.removeChild(div);    
  31. };
  32.  
  33.  
  34.  
  35. function Square(posX, posY, color, size)
  36. {
  37.         console.log("constructor square");              //Konsoleneintrag für Firebug
  38.        
  39.         PaintObj.call(this, posX, posY, color );        // Aufruf des Prototyp-Konstruktors
  40.         this.size = size;
  41. }
  42. Square.prototype = new PaintObj();      //Prototyp/Kopie/Klon von PaintObj für neues Quadrat
  43.  
  44.  
  45. Square.prototype.draw = function()
  46. {
  47.         console.log("constructor draw");                //Konsoleneintrag für Firebug
  48.        
  49.         this.createElement();
  50.         this.setClickHandler();
  51.        
  52.         this.div.style.width = this.size + "px";
  53.         this.div.style.height = this.size +"px";
  54. };
  55.  
  56.  
  57.  
  58.  
  59. Square.prototype.setClickHandler = function()
  60. {
  61.         this.div.onclick = function()
  62.                 {
  63.                         PaintObj.prototype.deleteElement(this); // this in dieser Zeile = div im DOM-Baum
  64.                 }
  65. }