tags:

views:

45

answers:

4

I have a Javascript class that contains

add remove removeall update . . . updateLik . .

functions.

And in my Serverside script a have Links like

Add

Now if user click on the DIV, the function "add" will be called. and the add function calls in turn the updateLink function.

updateLink replaces the onclick attribute to "myobject.delete(100)"

Now my problem is that in updateLink function i had to hardcode the objectname to call its delete function.

Is there any way to get the objectname, or any other solution? Thanks

A: 

Simplest way is to use eval.

var myObject = eval(myObjectName);

That is if you have the name of the object in a string format.

I would also look at at the YUI event library. It allows you to execute any function in any scope.

http://developer.yahoo.com/yui/event/

Zoidberg
+2  A: 

You could store a reference of the context where your object is created, and then search within it, looking for the actual instance:

function MyClass() {
  this.getVarName = function () { 
    for (var name in this.scope) 
      if (this.scope[name] === this) 
        return name;
  } 
}

MyClass.prototype.scope = this;

var myObject = new MyClass();
myObject.getVarName(); // returns "myObject"
CMS
A: 

Example:

var x = new YourClass();
var y = x;

Question: What is the name of your object? x or y?

Solution:

var x = new YourClass('x'); // and have your class copy the name

or

var x = new YourClass();
x.name = 'x';
Aaron Digulla
A: 

delete is a keyword so you cannot use it with dot-notation (object.method). You have to use ['property']-notation (object['method']).

var obj = {delete: function(){}}; // throws a syntax error
var obj = {'delete': function(){}}; // works

obj.delete() // throws a syntax error
obj['delete']() // works
Eli Grey