/    Sign up×
Community /Pin to ProfileBookmark

OOP Question…advanced ish

hey,
I’m writing some oo code and am wondering if there is a way to get an objects variable name, ie,

function someobj(id){
this.id=id;
this.displayId = displayId;
return this;
}

function displayId(){
with(this);
<—- I would like to be able to get the variable that the object is stored in (ie objX)—->
}

var objX = new someobj(‘0909’);

Thanks in advance
Carl

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FalconixMay 01.2003 — You could always just make a member of the class that holds the object's name, and have it set during the constructor, as a second arguement.
Copy linkTweet thisAlerts:
@ScriptageauthorMay 01.2003 — I thought as much,

Thanks for your time

Regards

Carl
Copy linkTweet thisAlerts:
@ScriptageauthorMay 01.2003 — Just came up with this code...it's sloppy but does the job.

Not sure about cross browser compatibility, but this seems to work well in IE. It is dynamic...just uses an eval to create a new object and sets varname as the variable name holder.

Could you please check it for cross browser compatibilty Dave?

This could be useful.

function display(){

with(this);

document.write("<b>Variable: </b><i>"+this.varname+"</i><br /><b>First name:</b><i>" + this.firstname + "</i><br><b>Last name:</b><i>" + this.lastname + "</i>");

}

function func1(firstname, lastname){

this.firstname = firstname;

this.lastname = lastname;

this.display = display;

return this;

}

[B]

function obj(varname, the_object,args){

eval(varname + " = new " + the_object +"(" +args +")");

eval(varname+".varname = ' "+varname+" ' ");

return this;

}

obj('person2',func1,"'Carl','Bates'");

[/B]


person2.display();
Copy linkTweet thisAlerts:
@VladdyMay 01.2003 — Why an object needs to know the name of it's instance when all the information stored in it is available through this :rolleyes: ? ?
Copy linkTweet thisAlerts:
@ScriptageauthorMay 01.2003 — say I have a function defined as this.display = display;

function somefunc(){

with(this);

document.write("<a href='#' onClick='"this.display"'>clicky</a>");

}

the above doesn't work!!!!!!

where as:

function somefunc(){

with(this);

document.write("<a href='#' onClick='"+this.varname+".display();'>clicky</a>");

}

would work!

That's why

Regards
Copy linkTweet thisAlerts:
@VladdyMay 01.2003 — If your object is associated with a HTML node (expanding it's functionality), you create the instance of the object within such node:

with(document.createElement('a'))

{ myExtension = new myExtensionObject();

onclick = function () { this.myExtension.processOnClick(); return false;};

}
×

Success!

Help @Scriptage spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...