WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Adding functions with object prototyping, Micha Schopman, September 29, 2003
     Re:Adding functions with object prototyping, Wellington, September 29, 2003
         Re:Adding functions with object prototyping, Micha Schopman, September 29, 2003
         Re:Adding functions with object prototyping, Micha Schopman, September 29, 2003
             Re:Adding functions with object prototyping, Erik Arvidsson, September 30, 2003
             Re:Adding functions with object prototyping, Wellington, September 29, 2003

Subject: Re:Adding functions with object prototyping From: Micha Schopman Date: September 29, 2003

it is a bit confusing for me what happens with this

this.newNode.obj = this;

I do not really get why something like

this.newNode.onclick = new Function("this.remove()");

does not work. When you see it like this, you expect you make a reference to node.remove(); But after all you could expect this.remove references to the div it is appended to.

I guess it is a mather of thinking to difficult. Quite annoying actually..

From: Wellington
Sent: September 29, 2003
Subject: Re:Adding functions with object prototyping

Hi, I found a solution:

<script type="text/javascript">
 nodes = new Array();
 count = 0;
 
 function node(label,parentNode){
  this.label = label;
  this.parentNode = parentNode;
  this.add();
 }
 
 node.prototype.add = function(){
  this.newNode = document.createElement("DIV");
  this.newNode.className = 'node';
  this.newNode.innerHTML = 'new node' + count;

       //necessary
  this.newNode.obj = this;

      //if the attachEvent isn't required
  this.newNode.onclick = new Function("this.obj.remove()");
     //else
  this.newNode.id = '__' + count;
  this.newNode.attachEvent("onclick", new Function("document.all['"+ '__' + count +"'].obj.remove()"));  

  this.parentNode.appendChild(this.newNode);
  count++;
 }
 
 node.prototype.remove = function(){
  alert(this.label);
 }

       //to test
  window.onload = function(){
      A = new node('AAAA', document.body); 
      AB = new node('AAAA', A.newNode); 
      AC = new node('AAAA', A.newNode); 
      AD = new node('AAAA', A.newNode);
   }

</script>





Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX