简体   繁体   中英

Javascript: Instance name in a class method

<html>
<body>
<div id="output"></div>

<script>
    function jExgTrend(){

    }

    jExgTrend.prototype.Start = function(text)
    {
        //this must return Instance name : "TestObj"
        var InstanceName = "TestObj";

        document.getElementById("output").innerHTML = "<a href=\"javascript:"+InstanceName+".Notify('"+text+"');\">"+text+"</a>";

    }

    jExgTrend.prototype.Notify = function(msg)
    {
        alert(msg);
    }

    var TestObj = new jExgTrend();
    TestObj.Start("Text of the link");

</script>


</body>
</html>

How can I do something like this ? the method "Start" should return the name of the Instance of the class.

The problem is stupid I know :-(

You can't. You can specify a name at instantiation though:

function JExgTrend(name){ this.name = name || 'no name specified'; }
JExgTrend.prototype.Start = function () {
                              alert(this.name);
                            }

var testObj = new JExgTrend('testObj');
var otherTestObj = new JExgTrend('otherTestObj');
var anon = new JExgTrend;
testObj.Start();      //=> testObj
otherTestObj.Start(); //=> otherTestObj 
anon.Start();         //=> no name specified

A somewhat exotic alternative: you could program the constructor like this:

function JExgTrend(name,scope) {
  name = name || ( Math.floor( 10000+Math.random()*100000000000) ).toString(16);
  if (!(this instanceof JExgTrend)) {
    return new JExgTrend(name,scope);
  }
  this.name = name;
  if (!JExgTrend.prototype.myname) { 
    JExgTrend.prototype.myname = function(){ console.log(this.name); };
  }
  return (scope || window)[this.name] = this;
}

And then assign objects like this:

jExgTrend.call(null, 'testObj');
testObj.myname(); //=> testObj

Try fiddling around @ this jsFiddle

Maybe what you really need is an id, not a name.
Providing an id is something you can easily add with a method that you can add to Object's prototype or just to the class(es) that interest you :

var idGetter = (function() {
       var currentId = 0;
       return function() {
               // 1. replace 'id' with a readonly property
               //      that will return id for this object
               var thisId = currentId ;
               Object.defineProperty( this, 'id', 
                       { get : function () { return thisId; } } ) ;
               // 2. for the first run of id, return object id
               return currentId++;
       }
}());

Object.defineProperty( Object.prototype, 'id', { get : idGetter } );

small example of use :

var someObject = {};
console.log(someObject.id);  // outputs 0

var someObject2 = {};
console.log(someObject2.id); // outputs 1

Notice that Object.defineProperty defaults to a non-enumerable property, so your objects won't get 'polluted' by this property (when using for..in for instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM