I am trying to write some javascript functions for a web application.
Following the advice from here: How do I declare a namespace in JavaScript?
I am trying to make the code unobtrusive by wrapping everything in named functions, however I am running into difficulty accessing properties in the parent function when inside a child anonymous function.
For example:
var title_picker = new function(){
// This property should be public
this.callback=function(){};
var ok=function(){
var title = $('input#title').val();
callback(title) // <---
}
...
When inside the “ok” function, what is the best way to reference the “callback” property?
With the code as written, no there isn’t.
You can access variables from the scope of the parent, unless they are overwritten in the narrower scope.
thisis always overwritten.You can copy
thisto another variable that remains in scope though: