Basic knowledge of js (public method, private method, privileged method)

Keywords: REST Attribute

Basic knowledge of js (public method, private method, privileged method)

  1. Public Method: A public method is a method that can be accessed and invoked externally.

    • It can be understood as creating a static method like Rest.

      //This definition is equivalent to ** static, accessible directly through Rest **.
      var Rest = {
       name: 'rest',
       // public Method
       getName: function() {
       return this.name;
       }
      }
      console.log(Rest.getName());//rest
    • Create classes and then invoke methods through objects

      // In the constructor
      function Person(name, age) {
       this.name = name;
       this.age = age;
       // public Method
       this.getName = function() {
       return this.name;
       }
      }
      // In the prototype
      Person.prototype.getAge = function() {
       return this.age;
      }
      var person = new Person("wangji",12);
      console.log(person.getName());//wangji
      console.log(person.getAge());//12
  2. Private and privileged methods
    These two methods are generally discussed together, because the privilege method we define refers to the public method that has access to internal private attributes and private methods (the method that can access private attributes is called privilege method, which is also one of public methods), and the private method refers to the method that is invisible and inaccessible outside.
    There are usually two ways to define an object
    One is to use Object instantiation or object expression
    The second is to use constructors.
    Private and privileged methods are also defined in different ways.

    In object

    Here we create an object by using Object object expression, add some attributes and methods, and then call it directly in a static way. For example, Rest.getName();

    Immediate Execution Function: Private data of an object is placed in an anonymous function Immediate Execution Expression (IIFE), which means that the function exists only at the moment of invocation and is destroyed immediately after execution.

    The way to create private data in an object is called module mode in the object's mode (referring to the mode of creating object). It can be called directly through static invocation, and returns the equivalent of a {} object, which can be called directly.

    var yourObject = (function() {
    
     // Private attributes and methods
     return {
     // Public methods and attributes
     }
    }) ();
    
    Like Rest, you can access it directly through yourObject. Such modular access is still quite powerful.

    Example

     var TaoBao = (function() {
     // Private attributes
     var _total = 10;
    
     // Private method
     var _buyFood = function() {
         _total--;
     }; 
     var _getTotal = function() {
        return _total;
     }
    
     return {
        name: 'taobao',
        getTotal: _getTotal,//Indirect use of internal private variables using closures
        buy: _buyFood
     }
    }) ();
    
    TaoBao.buy();
    console.log(TaoBao.name); // 'taobao'
    console.log(TaoBao.getTotal()); // 9

    Indirect use of internal private variables using closures

    In the constructor

    There is no essential difference between public and privileged methods when creating private methods in the module schema described above, because the concept is defined when using constructors to create private data.
    It's convenient to define private properties and methods in constructors. We don't need to use closures to initialize data when invoked.

    function TaoBao(name) {
     // Private attributes
     var _total = 10;
    
     // Public attribute
     this.name = name;
    
     // Private method
     function _buyFood() {
        _total--;
     }
    
     // Privileged methods allow access to private attributes and methods
     this.buy = function() {
         _buyFood();
     }
    
     this.getTotal = function() {
        return _total;
     }
    }
    
    // Public method, note that private member_total cannot be accessed here
    TaoBao.prototype.getName = function() {
        //console.log(_total); // Uncaught ReferenceError: _total is not defined
        return this.name;
    }
    
    var taobao = new TaoBao('taobao');
    console.log(taobao.getName()); // 'taobao'
    taobao.buy();
    console.log(taobao.getTotal()); // 9

    A more flexible way to integrate
    Using module mode, we can call it many times, and it will be destroyed after each execution.
    Constructors can pass in some initialized data, but private member attributes can not be accessed in public methods. If there are many public methods that need to access private data, we write them all in privileged ways. Finally, we bring many unnecessary methods to each instance.
    Therefore, the combination of the two can complement each other in length and in a simple way.

     var TaoBao = (function() {
     // Private attributes
     var _total = 10;
    
     // Private method
     function _buyFood() {
     _total--;
     }
    
     // Constructor
     function TaoBao(name) {
     this.name = name;
     this.getTotal = function() {
     return _total;
     }
     }
     //How can private methods be accessed here? This is not TaoBao's private property!
     TaoBao.prototype.buy = function() {
        console.log(_total); // 10
        _buyFood();
     }
    
     TaoBao.prototype.getName = function() {
        return this.name;
     }
    
     return TaoBao;
    }) ();
    
    var taobao = new TaoBao('taobao');
    console.log(taobao.getName()); // 'taobao'
    taobao.buy();
    console.log(taobao.getTotal()); // 9

    Great, get... .

Posted by Kardall on Wed, 10 Apr 2019 10:30:31 -0700