这是在 Javascript / node.js 中执行私有函数的正确方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32515793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is this the correct way to do private functions in Javascript / node.js?
提问by aussieshibe
A class I'm writing in node.js is as below:
我在 node.js 中编写的一个类如下:
module.exports = exports = function(){ return new ClassA() };
function ClassA(){
this.myvariable = 0;
}
I have a function that I want to be private. To my understanding if the function is declared outside of the constructor, it will essentially be a static function which wouldn't be able to reference this.myvariable.
我有一个想要私有的函数。据我了解,如果函数是在构造函数之外声明的,它本质上将是一个无法引用 this.myvariable 的静态函数。
Is the correct way of dealing with this to declare the function within the constructor like this:
处理这个在构造函数中声明函数的正确方法是这样的:
//within constructor
this.myFunction = function myFunction(){
console.log(this.myvariable)
}
Or is there a better way of doing it that doesn't leave me with a potentially huge constructor?
或者有没有更好的方法来做到这一点,而不会给我留下一个潜在的巨大构造函数?
EDIT: It looks like I've misunderstood something here because the above code doesn't even work...
编辑:看起来我在这里误解了一些东西,因为上面的代码甚至不起作用......
回答by Yuri Zarubin
Simplest way to have a private function is to just declare a function outside of the class. The prototype functions can still reference it perfectly fine, and pass their this
scope with .call()
拥有私有函数的最简单方法是在类之外声明一个函数。原型函数仍然可以完美地引用它,并将它们的this
作用域传递给.call()
function privateFunction() {
console.log(this.variable)
}
var MyClass = function () {
this.variable = 1;
}
MyClass.prototype.publicMethod = function() {
privateFunction.call(this);
}
var x = new MyClass();
x.publicMethod()
回答by intekhab
Whatever you will not add to module.exports
will be private for that module
and can not be accessed from outside of the module.
Also inside the controller store the reference of this
into a local variable
您不会添加的任何内容都module.exports
将是私有的,module
并且无法从模块外部访问。同样在控制器内部将 的引用存储this
到局部变量中
var self = this;
You can use revealing module pattern.
您可以使用揭示模块模式。
var myNameSpace = function() {
var current = null;
function init() {
…
}
function change() {
…
}
function verify() {
…
}
return{
init:init,
change:change
}
}();
module.exports = exports = myNameSpace;
This way init
and change
will be public
and verify will be private.
这样init
,change
将会public
和验证将是私有的。
You can see Douglas Crockford style of making private members. http://javascript.crockford.com/private.html
您可以看到 Douglas Crockford 制作私人会员的风格。 http://javascript.crockford.com/private.html
Edit
Douglas Crockford's link has been changed now.
new link http://crockford.com/javascript/private.html
编辑
Douglas Crockford 的链接现已更改。
新链接http://crockford.com/javascript/private.html
回答by Grégory NEUT
Update from May 17, 2019
2019 年 5 月 17 日更新
In ESNext, which is the next specification of javascript, the support of private methods and attributes has been added.
在 JavaScript 的下一个规范 ESNext 中,增加了对私有方法和属性的支持。
To make an attribute or a function private, use the character #
.
要将属性或函数设为私有,请使用字符#
。
class Foo {
#privateAttribute = 'hey';
showPrivate() {
console.log(this.#privateAttribute);
}
}
It has been implemented in node.js v12+.
它已在 node.js v12+ 中实现。
You can configure babel to transpile it using the following plugin.
您可以使用以下插件配置 babel 以对其进行转译。
It requires babel v7.0.0+.
它需要 babel v7.0.0+。
回答by aarav
Yes you can make a private method, but it can't be part of the prototype
是的,您可以创建私有方法,但它不能成为原型的一部分
function ClassA()
{
var myvariable;
var private = function() // This is private method
{
myvariable = 0;
}
this.public = function() // This is public method
{
private();
}
}
回答by Muhammad Usman
Javascript Public Functions
Javascript公共函数
ClassA.prototype.myFunction = function (string) {
//your logic
}
another type of public function
另一种公共职能
function ClassA(){
this.myvariable = 0;
var MyFunction3 = function () {
//your logic
};
this.MyFunction2 = function () {
//your logic
};
}
javascript Private Function
javascript私有函数
function ClassA() {
function MyFunction() {
//your logic
}
}
I would like to prefer to visit this linkthat will describe you every well.
回答by behtgod
In fact ,there are not class in javascript.
Look a json object constructor:
事实上,javascript中没有类。
看一个 json 对象构造函数:
var ClassA=function() {
this.publicFunction=function (){
console.log("public function");
}
function privateFunction(){
console.log("private function");
}
return this;
}
then you use new to create a ClassA:
然后你使用 new 创建一个 ClassA:
var a=new ClassA();
you can use the publicFunction, but you can't use the privateFunction like it's private.
您可以使用 publicFunction,但不能像私有一样使用 privateFunction。
a.publicFunction();
a.privateFunction();//you can't do this
Any help for you?
对你有什么帮助吗?