[英]Private static functions in javascript
How can I create a function that can't be called from outside?如何创建一个无法从外部调用的函数?
var obj = {
function1: function(){
alert("function1");
},
function2: function(){
alert("function2...");
obj.function1();
}
};
// so how to make this function unaccessible
obj.function1();
// and you could only call this function
obj.function2();
You may want to consider using the Yahoo Module Pattern .您可能需要考虑使用Yahoo Module Pattern 。 This is a singleton pattern, and the methods are not really static, but it may be what you are looking for:
这是一个单例模式,方法不是真正静态的,但它可能是您正在寻找的:
var obj = (function () {
//"private" variables:
var myPrivateVar = "I can be accessed only from within obj.";
//"private" method:
var myPrivateMethod = function () {
console.log("I can be accessed only from within obj");
};
return {
myPublicVar: "I'm accessible as obj.myPublicVar",
myPublicMethod: function () {
console.log("I'm accessible as obj.myPublicMethod");
//Within obj, I can access "private" vars and methods:
console.log(myPrivateVar);
console.log(myPrivateMethod());
}
};
})();
You define your private members where myPrivateVar
and myPrivateMethod
are defined, and your public members where myPublicVar
and myPublicMethod
are defined.您可以定义您的私有成员
myPrivateVar
和myPrivateMethod
定义,并在你的公共成员myPublicVar
和myPublicMethod
定义。
You can simply access the public methods and properties as follows:您可以简单地访问公共方法和属性,如下所示:
obj.myPublicMethod(); // Works
obj.myPublicVar; // Works
obj.myPrivateMethod(); // Doesn't work - private
obj.myPrivateVar; // Doesn't work - private
The simple answer is that you can't do both .简单的答案是你不能两者都做。 You can create
"private"
methods or "static"
methods, but you can't create Private static functions as in other languages.您可以创建
"private"
方法或"static"
方法,但不能像在其他语言中那样创建私有静态函数。
The way you can emulate privacy is closure :您可以模拟隐私的方式是关闭:
function f() {
function inner(){}
return {
publicFn: function() {},
publicFn2: function() {}
}
}
Here because of closure , the inner
function will be created every time you call f
, and the public functions can acces this inner
function, but for the outside world inner
will be hidden .这里,是因为封闭的
inner
功能将被创建每次通话时间f
和公共功能可存取权限此inner
功能,但对于外界inner
将被隐藏。
The way you create static methods of an object is simple:创建对象的静态方法的方式很简单:
function f() {}
f.staticVar = 5;
f.staticFn = function() {};
// or
f.prototype.staticFn = function() {};
Here the function object f
will only have one staticFn
which has access to static variables, but nothing from the instances .这里函数对象
f
将只有一个staticFn
可以访问静态变量,但没有来自instances 。
Please note that the prototype
version will be inherited while the first one won't.请注意,
prototype
版本将被继承,而第一个不会。
So you either make a private method that is not accessing anything from the instances, or you make a static method which you doesn't try to access from the outside.因此,您要么创建一个不从实例访问任何内容的私有方法,要么创建一个不尝试从外部访问的静态方法。
You can use a closure, something along the lines of....您可以使用闭包,类似于....
var construct = function() {
var obj = {};
var method1 = function() {
alert("method1");
}
obj.method2 = function() {
alert("method2...");
method1();
}
return obj;
}
obj = construct();
Then:然后:
obj.method2
is accessible, but obj.method1
doesn't exist for public usage. obj.method2
是可访问的,但obj.method1
不存在供公共使用。 It can be only accessed using member functions of the class.它只能使用类的成员函数访问。
Objects can be produced by constructors , which are functions which initialize objects.
对象可以由构造函数生成,构造函数是初始化对象的函数。 Constructors provide the features that classes provide in other languages, including static variables and methods.
构造函数提供类在其他语言中提供的功能,包括静态变量和方法。
Read all about it at http://www.crockford.com/javascript/private.html在http://www.crockford.com/javascript/private.html阅读所有相关信息
You can do it like this:你可以这样做:
var obj = new function() {
var method1 = function() { // private }
this.method2 = function() { // public }
}
obj.method1(); // not possible
obj.method2(); // possible
Note that I also use an anonymous constructor.请注意,我还使用了匿名构造函数。 This is sort of analogous to the Singleton pattern.
这有点类似于单例模式。
Maybe you want a proxy object containing only the public methods, eg也许你想要一个只包含公共方法的代理对象,例如
var obj = (function() {
var obj = {
method1: function(){
alert("method1");
},
method2: function(){
alert("method2...");
obj.method1();
}
}
return {
method2: function() { return obj.method2.apply(obj, arguments) }
}
})()
// you could only call this function
obj.method2();
// this function inaccessible
obj.method1();
I don't see the point of private methods in javascript.我没有看到 javascript 中私有方法的意义。 If you don't want people calling a method then don't advertise it.
如果您不希望人们调用某个方法,请不要宣传它。 Private methods also make debugging just that bit harder too.
私有方法也使调试变得更加困难。
This is how it really should be done.这才是真正应该做的。
var MyClass = new(function() {
var privateStaticVariable = '';
function privateStaticMethod() {
}
function construct() {
var self = this;
this.publicMethod = function() {};
function privateMethod() {
//maybe lest use our private static method
privateStaticMethod();
}
}
construct.publicStaticVariable = '';
construct.publicStaticMethod = function() {};
return construct;
})();
Now lets use it:
var myClass = new MyClass();.....
MyClass.publicStaticMethod();
MyClass.publicStaticVariable = 'sfdsdf';
myClass.publicMethod();
..... .....
Use class
.使用
class
。 Though some of the features are still not shipped as it's at Stage 3 .尽管某些功能仍然没有像第 3 阶段那样发布。
const instance = new (class Alpha { static #private_static_method() { console.info("I am private and static."); } static public_static_method() { Alpha.#private_static_method(); console.info("I am public and static."); } #private_nonstatic_method() { console.info("I am private and non-static."); } public_nonstatic_method() { this.#private_nonstatic_method(); console.info("I am public and non-static."); } }); instance.constructor.public_static_method(); // instance.constructor.#private_static_method(); // impossible instance.public_nonstatic_method(); // instance.#private_nonstatic_method(); // impossible
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.