跳到主要内容
CNode

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

问答
RRooQs发布于 8 年前最后回复 8 年前
951500

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

"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)

X
xtx1130#98 年前
const privateVar = Symbol.for('pravite#var')
D
dislido#88 年前

image.png

B
blackjack#78 年前

sybom

Z
zswnew#68 年前

用Proxy,调用时检测属性

来自酷炫的 CNodeMD

Z
zhhb#58 年前

typescript 了解下

C
cd-xulei#48 年前

module 不导出的就是私有方法

A
atian25#38 年前

学习下 markdown 排版吧。。。

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

const _hide = Symbol('hide');
class Test {
  [_hide] () {
    console.log('I\'m private');
  }
  sayHi() {
    this[_hide]();
  }
}
I
im-here#28 年前

ES6不提供私有方法的

F
fhawk#18 年前

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

参与回复

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