CNode

Nodejs 后端程序 class 怎么实现私有方法

问答
RRooQs发布于8 年前最后回复8 年前9 回复5109 浏览0 收藏

搞了半天不知道怎么办 自己写的代码看下 可行吗

"use strict"

const testF=function (){ console.log(this.name) }

class Test{

constructor(name){
    this.name=name
}

test(){
   testF.bind(this)()
}

}

let t=new Test('小明')

t.test()

查看回复

回复 (9)

F
fhawk#1·8 年前

function testF(){ function xx(){}; } testF.prototype.a = function(){ this.xx(); }; module.exports = testF; 这里面xx()是私有方法,a()是公共方法。

I
im-here#2·8 年前

ES6不提供私有方法的

A
atian25#3·8 年前

学习下 markdown 排版吧。。。

private 的提案还没落地,现在的话一般用 Symbol 来模拟

const _hide = Symbol('hide');
class Test {
  [_hide] () {
    console.log('I\'m private');
  }
  sayHi() {
    this[_hide]();
  }
}
C
cd-xulei#4·8 年前

module 不导出的就是私有方法

Z
zhhb#5·8 年前

typescript 了解下

X
xtx1130#6·8 年前
const privateVar = Symbol.for('pravite#var')
Z
zswnew#7·8 年前

用Proxy,调用时检测属性

来自酷炫的 CNodeMD

B
blackjack#8·8 年前

sybom

D
dislido#9·8 年前

image.png

参与回复
登录后即可参与回复。登录