Data Encapsulation in Javascript private fields

encapsulation

Data Encapsulation has been around for a long time, preventing data certian exposure for various reasons such as protecting sensitive data,providing control through restricted functions. Let us consider a scenario of a company and its communication channels. Public notices are allowed for the general public, however sensitive internal information must require authentication.

class Company {
		 publicnotifications = ["Launching next week"];
		 employeenotifications = ["Get Launchpad ready", "Test Lauch code is 9876"];  
		
		 getInternalNotifications(passcode){
			if(passcode != "secretpwd") return "possible security breach";        // does he have permission?
			return  this.#employeenotifications;
		 }
	 }

	var company = new Company();

	//public
	console.log(company.publicnotifications); // yes thats allowed
	// Output : ["Launching next week"]

	company.getInternalNotifications(); // yes block him without correct password
	// Output : "possible security breach"

	console.log(company.employeenotifications); // OH Nooooo ! Call off the launch !!!
	//Output :  ["Get Launchpad ready", "Test Lauch code is 9876"]

In this way certian private varaiable may be revealed. Ofcourse encapsulation could be implemented through closure functions but thats extra code to work with, any new additions need to be exposed, etc. With the addition of private fields it is easy to implement a private variable in Javascript too. Let us modify the same function.

class Company {
		 publicnotifications = ["Launching next week"];
		 #employeenotifications = ["Get Launchpad ready", "Test Lauch code is 9876"];   // privatize this field with a # key

		 getInternalNotifications(passcode){
			if(passcode != "secretpwd") return "security breach";        
			return  this.#employeenotifications;
		 }
	}
	var company = new Company();
	//public
	// console.log(company.employeenotifications);
	// Output : undefined

	//employees with pwd
	console.log(company.getInternalNotifications("secretpwd"));
	//Output :  ["Get Launchpad ready", "Test Lauch code is 9876"]

Well what about private fiunctions. Well, in the same way functions can be privatized with a Hash(#) prefix

class Company {
		 ...
		 getInternalNotifications(passcode){      
			if(! this.#isUserAuthenticated(passcode)) return "security breach";        
			return  this.#employeenotifications;
		 }

		//Private function
		 #isUserAuthenticated(passcode){
			return (passcode == "secretpwd");
		 }
	}

Conclusion, relate it to real world examples and put it to practice.