Details about how to create privileged methods and js privileges in the js private scope
This article provides examples of how to create a privilege in the js private scope for your reference. The details are as follows:
Privileged MethodsIs the public method that has the right to access private variables and private functions:
function MyObject(){ var privateVariable = 10; function privateFunction(){ return false; } this.publicMethod = function(){ privateVariable ++; return privateFunction(); };} var x = new MyObject();console.log(x.publicMethod()) ;//false
Private variables and functions are defined in the private scope. You can also create privileged methods, such:
(Function () {var privateValue = 10; function privateFunction () {return false;} MyObject = function () {}; // No var is a global variable, in strict mode, MyObject is reported. prototype. publicMethod = function () {privateValue ++; return privateFunction () ;}}) (); var instance = new MyObject (); console. log (instance. publicMethod ());
Here we can see that a global build function is defined in the private scope. One of the methods is to return a private variable and attribute in the private scope. Write it as follows to better understand:
Obj = function(){};(function(){ var x = 10; function y(){ return x + 10; } Obj.prototype.say = function(){ console.log(y()); };})()var ins = new Obj();ins.say();
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- Js variables and their scopes
- Javascript object-oriented (1) (common methods, private methods, privileged methods)
- Scope chains and closures in JavaScript
- Definition of JS privileged methods and their differences with public methods
- Understanding and using js scopes and scope chains
- Use of sessions and scopes in JSP
- Summary of javascript Functions and scopes
- Overview of the scope and context in javascript
- A deep understanding of the lexical scopes and scopes of JavaScript
- Basic js knowledge (public, private, and privileged)