Home Web Front-end JS Tutorial How Can We Avoid Global Namespace Pollution in JavaScript?

How Can We Avoid Global Namespace Pollution in JavaScript?

Nov 27, 2024 am 01:14 AM

How Can We Avoid Global Namespace Pollution in JavaScript?

Deciphering Global Namespace Pollution

Global namespace pollution occurs when excessive global variables are declared, potentially leading to conflicts and reduced code readability.

Impact on Garbage Collection

Global variables persist until the global namespace loses scope, making them ineligible for garbage collection. This can lead to memory leaks and performance issues, especially with large data sets.

Abusing the Global Namespace

Creating multiple global variables is considered abusive behavior. It can result in naming collisions, overwriting, and confusion.

Example: Bad Practice

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

var x1 = 5;

var x2 = 20;

var y1 = 3;

var y2 = 16;

 

var rise = y2 - y1;

var run = x2 - x1;

 

var slope = rise / run;

 

var risesquared = rise * rise;

var runsquared = run * run;

 

var distancesquared = risesquared + runsquared;

 

var distance = Math.sqrt(dinstancesquared);

Copy after login

This creates 11 global variables that can potentially interfere with other global variables.

Resourceful Approach

The module pattern provides a better solution by encapsulating variables and methods within a single global object. This prevents other code from accessing or modifying the encapsulated variables, protecting the global namespace.

Example: Improved Approach

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

var Calculate = function () {

  // Local variables

  var Coordinates = [];

  var Coordinate = function (xcoord, ycoord) {

    this.x = xcoord;

    this.y = ycoord;

  };

 

  return {

    // Exposed methods

    AddCoordinate: function (x, y) {

      Coordinates.push(new Coordinate(x, y));

    },

 

    Slope: function () {

      var c1 = Coordinates[0];

      var c2 = Coordinates[1];

      return (c2.y - c1.y) / (c2.x - c1.x);

    },

 

    Distance: function () {

      // Local calculations

      var c1 = Coordinates[0];

      var c2 = Coordinates[1];

 

      var rise = c2.y - c1.y;

      var run = c2.x - c1.x;

 

      var risesquared = rise * rise;

      var runsquared = run * run;

 

      var distancesquared = risesquared + runsquared;

 

      var distance = Math.sqrt(distancesquared);

 

      return distance;

    }

  };

};

 

// Self-executing closure

(function () {

  var calc = Calculate();

  calc.AddCoordinate(5, 20);

  calc.AddCoordinate(3, 16);

  console.log(calc.Slope());

  console.log(calc.Distance());

})();

Copy after login

This approach reduces global pollution by limiting access to variables and methods within the Calculate object.

The above is the detailed content of How Can We Avoid Global Namespace Pollution in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

See all articles