This article shares the JS private scope of the creation of privileged methods for your reference, the specific contents are as follows
A privileged method is a public method that has access to private variables and private functions:
function MyObject () {
var privatevariable = ten;
function Privatefunction () {return
false;
}
This.publicmethod = function () {
privatevariable + +;
return privatefunction ();}
;
}
var x = new MyObject ();
Console.log (X.publicmethod ());//false
Private scopes can also create privileged methods, such as:
(function () {
var privatevalue = ten;
function Privatefunction () {return
false;
}
MyObject = function () {}; No var belongs to the global variable, the strict mode will error
MyObject.prototype.publicMethod = function () {
privatevalue + +;
return privatefunction ();
};
}) ();
var instance = new MyObject ();
Console.log (Instance.publicmethod ());
Here you can see that in fact, a global constructor is defined in the private scope, one of which is to return a private variable and property in a private scope. Write the following so you can better understand:
OBJ = function () {};
(function () {
var x = ten;
function y () {return
x +;
}
Obj.prototype.say = function () {
console.log (y ());
};
}) ()
var ins = new Obj ();
Ins.say ();
The above is the entire content of this article, I hope to help you learn.