Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting id attribute in subclass

    Hello!

    I have a superclass where I create a "div" element as a property and then proceed to try and set the element's "id" attribute in a subclass.

    Interestingly, if I create multiple instances of the subclass with different "id"s, they all end up having the last "id" I specify. I would like it if they retain the individual "id" I specify.

    Here is the code.

    Code:
    function Drag() {
    	this.ele = document.createElement("div"); }
    
    function Icon(id) {
    	this.ele.id = id; }
    
    Icon.prototype = new Drag();
    
    var hello = new Icon("hello");
    var goodbye = new Icon("goodbye");
    Last edited by willat8; 12-21-2009 at 07:33 AM. Reason: Thought I'd discovered something, but it turned out I hadn't

  • #2
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the purposes of what I am trying to do, I've solved it.

    I think the issue was that when "Icon" asked for "this.ele", it always received the same object, as the "ele" property was defined in "Drag" once, not every time a new instance of "Icon" was created. Making changes to this same object meant that all instances of "Icon" reflected these changes.

    My solution is to call a method in "Icon" which creates a new "this.ele" for use by the current instance.

    Code:
    function Drag() {
    	this.createNew = function() {
    		this.ele = document.createElement("div"); } }
    
    function Icon(id) {
    	this.createNew();
    	this.ele.id = id; }
    
    Icon.prototype = new Drag();
    
    var hello = new Icon("hello");
    var goodbye = new Icon("goodbye");

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    The reason it didn't work is because all Icon instances (as you have it) use the same prototype (a single Drag object). Thus when you change the prototype, the changes get shared amongst all objects that share the same prototype. But you have the right track in your second post. But there is a better and cleaner way.

    This is what you want to do:
    Code:
    function Drag() {
      this.ele = document.createElement("div");
    }
    
    function Icon(id) {
      Drag.call (this, id);
      this.ele.id = id;
    }
    
    Icon.prototype = new Drag();
    
    var hello = new Icon("hello");
    var goodbye = new Icon("goodbye");
    With inheritance you should use prototypes to inherit functionality and SuperClassFunction.call(this, arg1OfSuperClass, arg2OfSuperClass, ...) to instantiate state.

    For more info on Function.prototype.call:
    https://developer.mozilla.org/en/Cor.../Function/call
    Last edited by Trinithis; 12-21-2009 at 08:19 AM.
    Trinithis


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •