tags:

views:

42

answers:

2

Hi I the following functions in separate js files

//in js file 1
setUsername: function(v){
document.Load.LogUsername = v;
}
//in js file 2{wrapper for js file 1}
LogUserName: function(v)
{
  return myobject.SetLoguserName(v);
}

When document.Load.LogUsername = v; is called i need it to call the LogUserName function. LogUserName is a wrapper for setUsername. Any ideas how to get this to work. I know if change document.Load.LogUsername = v; to document.Load.LogUsername(v); then it works but i was asked not to change the js file 1

+1  A: 

You need to save the original function, like this:

var originalLogUserName = document.Load.LogUsername;
LogUserName: function(v)
{
    return originalLogUserName.call(myobject, v);
}
SLaks
Thanks for the speedy reply but i was asked not to modify the original function
wolv
@wolv: This is not modifying the original function. This code can run in the second script and simply saves the original function to a variable. You can then call the function from the variable.
SLaks
+1  A: 

You need to use a setter. E.g.:

document.Load = 
{
  set LogUsername(v) 
  {
    myobject.SetLoguserName(v);
  }
}; 
Matthew Flaschen
thanks can you explain with example i am new to javascript
wolv
@SLaks, [this post](http://robertnyman.com/2009/05/28/getters-and-setters-with-javascript-code-samples-and-demos/) says Firefox, Safari, Chrome, and Opera all support this syntax. I verified in Chrome.
Matthew Flaschen
@Matthew: I didn't know that; thanks.
SLaks
@Matthew: While all those browsers implement this syntax, I'm pretty sure it will be deprecated soon, in favor of the ES5 `Object.defineProperty`, `Object.defineProperties` and `Object.create` methods, which all use plain objects as property descriptors (no new syntax introduced :).
CMS