“Namespace in JavaScript” Code-Antworten

Namespace in JavaScript

JavaScript does not provide namespace by default. However, we can replicate 
this functionality by making a global object which can contain all functions 
and variables.

Syntax:
    To initialise an empty namespace
     	var <namespace> = {}; 
    To access variables in the namespace
     	<namespace>.<identifier>
        
Example:
	var car = {
    	startEngine: function () {
        	console.log("Car started");             
    	}        
	}
  
	var bike = {
    	startEngine: function () {
        	console.log("Bike started");
    	}
	}
    
car.startEngine();
bike.startEngine();

Output:
	Car started
	Bike started
rng70

Namespace JavaScript

// JavaScript does not provide namespace by default. 
// However, we can replicate this functionality by making 
// a global object which can contain all functions and 
// variables.
var car = {
    startEngine: function () {
        console.log("Car started");             
    }        
}
  
var bike = {
    startEngine: function () {
        console.log("Bike started");
    }
}
  
car.startEngine();
bike.startEngine();
Homemadesteam58

Ähnliche Antworten wie “Namespace in JavaScript”

Fragen ähnlich wie “Namespace in JavaScript”

Weitere verwandte Antworten zu “Namespace in JavaScript” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen