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 01-17-2009, 08:21 AM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
new object name

Code:
function Car()
{
  alert('car');
}

car = new Car();
the red part is what I want to alert out, but not as string constant, it should be something like

Code:
alert(this.name);
how to do that ?
BubikolRamios is offline   Reply With Quote
Old 01-17-2009, 12:57 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
As a function name cannot be changed there is no easy way to access it.

The following is somewhat contrived (not sure if it is really what you want):-

Code:
<script type = "text/javascript">

var y = function Car() {

y = y.toString();
var len = y.length;
var z = y.substring(9,len);
var w = z.indexOf("(");
z = z.substring(0,w);
alert (z);  // Car
}

Car()

</script>

"The only function of economic forecasting is to make astrology look respectable". - J.K.Galbraith
Philip M is offline   Reply With Quote
Old 01-17-2009, 03:31 PM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
firefox and chrome will return the name from a constructor's name property-
var c1=new Car;
alert(c1.constructor.name)

Most browsers return undefined, however.

You could define a name property in each constructor,
or dedicate a method to return any object's constructor name.

Code:
function classof(what){
	var tem, C= what.constructor;
	tem= C? C.toString().match(/function\s+([a-zA-Z][\w\$]+)\s*\(/): '';
	return tem? tem[1]: '';
}
Code:
//test case
function Car(specs){
	//
}
var mycar= new Car();
alert('classof(mycar)='+ classof(mycar));

// it works as well for native objects
alert(classof(/\w/g));

Last edited by mrhoo; 01-17-2009 at 03:38 PM..
mrhoo is offline   Reply With Quote
Old 01-17-2009, 05:06 PM   PM User | #4
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Nope, didn't make typo want to get the red part as it is(small caps)
Code:
function Car()
{
  alert('car');
}

car = new Car();
as faras I know the only way to do it is:

Code:
car = new Car('car');

function Car(derivateName)
{
  alert(derivateName);
}
Example of purpose of all this if it would work:

Code:
function Car()
{
  this.publicF = function()
  {}  

  tmpInnerHTML = "<DIV onclick = '" + this.name + ".publicF()'"

  someObj.innerHTML = tmpInnerHTML;

}

car = new Car();
car5 = new Car();
car100 = new Car();
//.....
any other idea ?

Last edited by BubikolRamios; 01-17-2009 at 05:14 PM..
BubikolRamios is offline   Reply With Quote
Reply

Bookmarks

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 10:30 AM.


Advertisement
Log in to turn off these ads.