Javascript private member on prototype

前端 未结 8 921
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 01:15

Well I tried to figure out is this possible in any way. Here is code:

a=function(text)
{
   var b=text;
   if (!arguments.callee.prototype.get)
      argumen         


        
8条回答
  •  离开以前
    2020-12-08 02:07

    I have created a new library for enabling private methods on the prototype chain. https://github.com/TremayneChrist/ProtectJS

    Example:

    var MyObject = (function () {
    
      // Create the object
      function MyObject() {}
    
      // Add methods to the prototype
      MyObject.prototype = {
    
        // This is our public method
        public: function () {
          console.log('PUBLIC method has been called');
        },
    
        // This is our private method, using (_)
        _private: function () {
          console.log('PRIVATE method has been called');
        }
      }
    
      return protect(MyObject);
    
    })();
    
    // Create an instance of the object
    var mo = new MyObject();
    
    // Call its methods
    mo.public(); // Pass
    mo._private(); // Fail
    

提交回复
热议问题