js basics (public method private method privileged method)

  • 2020-10-07 18:33:34
  • OfStack

The topic covered in this article, though basic and a gimmick for many, is a comprehensive one in JavaScript Basics. Knowledge of encapsulation of object properties, prototyping, constructors, closures, and immediate expression execution is covered here.

Public methods
A public method is a method that can be accessed and called externally.


//  In the object 
var Restaurant = {
 name: 'McDonald',
 //  Public methods 
 getName: function() {
 return this.name;
 }
}

//  In the constructor 
function Person(name, age) {
 this.name = name;
 this.age = age;
 //  Public methods 
 this.getName = function() {
 return this.name;
 }
}

//  In the prototype 
Person.prototype.getAge = function() {
 return this.age;
}

Private methods and privileged methods
The two methods are generally discussed at the beginning of method 1 because we define privileged methods to be public methods that have access to internal private properties and private methods, and private methods to be externally invisible and inaccessible.

There are usually two ways to define an object, one using Object instantiation or object expression, and two using constructors. Also, private and privileged methods are defined in different ways in different ways.

In the object
Here we create an object using an Object object expression and add 1 more properties and methods, then call it statically. The object's private data is placed in an anonymous function immediate execution expression (IIFE). This means that the function exists only at the moment it is called and is destroyed immediately after execution.

The way private data is created in an object is called the module pattern in the pattern of objects (the pattern of object creation), and its basic format is as follows:


var yourObject = (function() {

 //  Private properties and methods 

 return {
 //  Public methods and properties 
 }
}) ();

In module mode, the object literals returned contain only properties and methods that can be exposed.


var Restaurant = (function() {
 //  Private property 
 var _total = 10;

 //  Private methods 
 var _buyFood = function() {
 _total--;
 };
 var _getTotal = function() {
 return _total;
 }

 return {
 name: 'McDonald',
 getTotal: _getTotal,
 buy: _buyFood
 }
}) ();

Restaurant.buy();
console.log(Restaurant.name); // 'McDonald'
console.log(Restaurant.getTotal()); // 9

Note that we used a closure to indirectly use the internal private variable while initializing the restaurant (Restaurant) name (name).

In the constructor
When creating private methods in the module pattern described above, there is no fundamental difference between public and privileged methods, because the concept comes from being defined when creating private data using constructors.

It's convenient to define private properties and methods in constructors, so we don't need to use closures, and we can initialize the data at call time.


function Restaurant(name) {
 //  Private property 
 var _total = 10;

 //  Public attribute 
 this.name = name;

 //  Private methods 
 function _buyFood() {
 _total--;
 }

 //  Privilege method 
 this.buy = function() {
 _buyFood();
 }

 this.getTotal = function() {
 return _total;
 }
}

//  Public methods ,  Note that private members cannot be accessed here _total
Restaurant.prototype.getName = function() {
 console.log(_total); // Uncaught ReferenceError: _total is not defined
 return this.name;
}

var McDonald = new Restaurant('McDonald');
console.log(McDonald.getName()); // 'McDonald'
McDonald.buy();
console.log(McDonald.getTotal()); // 9

2 equals 1, which is a more flexible way
Using module mode, we can make multiple calls that are destroyed after each execution. Some initialized data can be passed in using the constructor method, but private member properties cannot be accessed in public methods. If there are many public methods that need to access private data, we all write them in privileged methods, which will end up bringing many unnecessary methods to each instance. Therefore, the combination of the two at 1 can be short and long complementary, combined in a very simple way


var Restaurant = (function() {
 //  Private property 
 var _total = 10;

 //  Private methods 
 function _buyFood() {
 _total--;
 }

 //  The constructor 
 function restaurant(name) {
 this.name = name;
 this.getTotal = function() {
 return _total;
 }
 }

 restaurant.prototype.buy = function() {
 console.log(_total); // 10
 _buyFood();
 }

 restaurant.prototype.getName = function() {
 return this.name;
 }

 return restaurant;
}) ();

var McDonald = new Restaurant('McDonald');
console.log(McDonald.getName()); // 'McDonald'
McDonald.buy();
console.log(McDonald.getTotal()); // 9

Above is the whole content of this article, this site only summarizes 1 part of it, there are many not mentioned knowledge points, you can explore their own research, hope this article can be helpful to beginners.


Related articles: