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 4 of 4

Thread: new object name

  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    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 ?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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.


  •  

    Posting Permissions

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