Reputation: 121
i'm following along a book and I have 2 javascript files. Both are in the HTML file as:
<script src="playlist_store.js"></script>
<script src="playlist.js"></script>
however, when I try to call a function from playlist_store.js inside of playlist.js chrome debugger says "uncaught reference error. loadPlaylist is not defined. loadPlaylist() is the function from playlist_store.js
I thought there might be a typo somewhere but I don't think there is. Does playlist.js actually need to somehow import or include / require the playlist_store.js ? Is the browser not smart enough to somehow link them so the functions in one you can call from another?
Upvotes: 0
Views: 1261
Reputation: 3848
Defining a function like follows
function myaction () { }
is a named (private) function.
It is better to write a (private) function assigned to a variable:
var myaction = function () { };
Now what you need is a function, that is assigned to a variable in the global object:
myaction = function () { };
// or strict:
window.myaction = function () { };
It is now a global (public) function, so you can use it outside of your defining .js.
Beware that the first example, a function statement doesn't have a semicolon. Using the function operator as in the other examples, you need a semicolon.
Upvotes: 2