Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-20-2009, 09:57 PM   PM User | #1
willat8
New to the CF scene

 
Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
willat8 is an unknown quantity at this point
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 06:33 AM.. Reason: Thought I'd discovered something, but it turned out I hadn't
willat8 is offline   Reply With Quote
Old 12-21-2009, 06:33 AM   PM User | #2
willat8
New to the CF scene

 
Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
willat8 is an unknown quantity at this point
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");
willat8 is offline   Reply With Quote
Old 12-21-2009, 07:13 AM   PM User | #3
Trinithis
Regular Coder

 
Join Date: Jun 2007
Location: USA
Posts: 527
Thanks: 26
Thanked 74 Times in 72 Posts
Trinithis will become famous soon enough
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 07:19 AM..
Trinithis is offline   Reply With Quote
Reply

Bookmarks

Tags
class, inheritance, prototype

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:03 AM.


Advertisement
Log in to turn off these ads.