JavaScript prototype lifecycle

If I want to add the isEmpty method for all JavaScript arrays, I would use the following code

Array.prototype.isEmpty = function() {
  return this.length == 0;
}

Suppose this code is in the file foo.js. If I want isEmpty to be available on all pages of a website, would I need to include foo.js in all HTML files? In other words, should prototypes get "reset" when the user goes to another page?

Thanks Don

+3
source share
3 answers

Yes, you need to include your code when loading each page.

/. Javascript 1 . , , Javascript .


1. " "

+5

,

+3

, http , .

Array.prototype . , , .

0

Source: https://habr.com/ru/post/1704313/


All Articles