If I want to add an isEmpty method to all JavaScript arrays, I would use the following code
Array.prototype.isEmpty = function() { return this.length == 0; }
Assume this code is in a file foo.js. If I want isEmpty to be available on all pages of a web site, would I need to include foo.js in all the HTML files? In other words, do the prototypes get ‘reset’ whenever the user navigates to a different page?
Thanks, Don
Yes, you wil need to include your code on each page load.
Think of each page load as a compile/linking cycle. All the various bits of Javascript on the page are linked together1 and then executed as one giant program. The next time a page is loaded, the default Javascript objects start in a fresh state.
1. Linked together in a brain-dead ‘every piece of code shares the same global namespace’ fashion