0

Below is my sample js in which everything is defined inside doDomReady function their are multiple function their. `

YAHOO.namespace("YAHOO.User");
YAHOO.User = (function() {
Event.onDOMReady(UserData = function() {
.......
function save(){}
..........
});

})();`

From the above js file I want to call the save method from outside(from other js file) like this ->YAHOO.User.save(resultset) but I am not able to call it since it is not visible.

Anyone tell me how to call the functions in above case.

Harshal Patil
  • 349
  • 1
  • 7
  • 26

1 Answers1

1
window.save == function(resultset){ ... }

This puts it in the global scope, so you could just call save() from another script. To namespace it under YAHOO.User, I suppose it would be:

window.YAHOO.User.save = function(resultset){ ... }

... then you can call YAHOO.User.save(resultset) from outside.

Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
  • It might be silly question but I am new so asking what does window refer here? – Harshal Patil Dec 03 '13 at 11:47
  • 1
    Good question! `window` is the parent object of an HTML document. Every object you create in HTML or javascript is a descendant of the `window` object, whether directly on window e.g. `window.myFunction` or under a sub-property of window, e.g. `window.myNameSpace.myFunction`. Here's a somewhat helpful diagram: http://stackoverflow.com/questions/8285895/javascript-dom-object-diagram – Josh Harrison Dec 03 '13 at 12:04
  • If you create variables in JavaScript, they will have the scope of the object (or function) that they are declared inside. If they are not declared inside any object or function, they will have the scope of `window` (the global scope). – Josh Harrison Dec 03 '13 at 12:08
  • 1
    There's a great answer here with some examples of different scopes: http://stackoverflow.com/a/500459/940252 – Josh Harrison Dec 03 '13 at 12:08