1

In context I have two javascript files, one handles the actual page itself while another handles a popup tutorial. Each script file is nested within a function as shown below:

(function () {
    'use strict';
    // script here
}());

In the main script is the following function:

function updateCompView(ident) {
    'use script';
    // code is here
}

However what I'm finding is that if I reference this function within the second script file (for the tutorial) like so:

updateCompView('KL-COM-001');

I get an Uncaught ReferenceError. I have managed to resolve this by removing the top-level function however this then means I get a large number of 'use strict' JSLint errors.

Is there a way to reference functions that are nested within an external function?

2
  • 3
    You'll probably want to read up on scopes and closures in JS. Commented Jun 4, 2014 at 10:33
  • 1
    You'll have to make it referenceable, for example, do if (!window.myNamespace) window.myNamespace = {} then set window.myNamespace.updateCompView = updateCompView; and now you can access it as window.myNamespace.updateCompView from outside Commented Jun 4, 2014 at 10:41

1 Answer 1

2

You'll have to make it referenceable

There are three ways to do this, so lets start assuming you have the following code

(function () { // this is an IIFE
    "use strict";
    function baz() {}
}()); // this is where it gets invoked

Method 1, make your own namespace (just an Object), at the beginning of the IIFE,

if (!window.myNamespace)
    window.myNamespace = {};

then after baz is defined,

window.myNamespace.baz = baz;

Now you can access baz via window.myNamespace.baz under other closures


Method 2, return out of an IIFE, at the end of the IIFE you do

return baz; // or some object containing baz

and you refactor your IIFE to look like this

var foo = (function () {
    // IIFE body
}());

Now you can access baz via foo in the closure and descendant closures of where foo was vard


Method 3, set baz in the global namespace

There are two options for this, one is to structure it as a function expression and don't var the variable you assign to, i.e.

baz = function () {};

The other way is similar to method 1 but without the namespace. This can be considered bad in that it is polluting the global namespace with something new each time you do it. After baz is defined,

window.baz = baz;

Now you can access baz via window.baz or simply baz if it's not shadowed under other closures

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, works perfectly. I had a vague idea of why it wasn't working but a lot of the posts online were overly complicated. This is the exact answer I was looking for, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.