我正在使用一些ES6功能,并尝试将我的一些旧代码的类结构转换为等效的ES6类。
var App = function() {
/**
* Public method calling a private method.
*/
this.init = function() {
events();
};
/**
* Private method.
*/
var handleClickEvent = function() {
console.log('Click handled');
};
/**
* Private method with event calling private
* method in this class.
*/
var events = function() {
document.addEventListener('click', handleClickEvent);
};
};
(new App).init();
这是通过制作方法static
还是有private
等价的正确方法,也可以在类之外调用静态方法,如App.events()
?
class App {
constructor() {
App.events();
}
static handleClickEvent() {
console.log('Click handled');
}
static events() {
document.addEventListener('click', App.handleClickEvent);
}
}
new App();