问题描述
我只是试图ES6,并想要重写一部分的代码写在常规javascript到ES6。现在,我试图在ES6类中重写私有属性和方法时卡住了。看起来ES6中的类没有显式地提供任何东西来拥有私有数据或方法。
此外,我查看了此主题:,发现我们可以使用存储私人数据。这是一种奇怪,但它仍然可以是一个工作。我设法使用它的私人数据。
但是私人方法呢?在ES6类中使用私有方法(或甚至受保护的方法)的推荐方法是什么?
如果任何人都可以向我展示干净方式,使用ES6类和私有方法重写这部分代码,我将非常感激。
谢谢。
这是原来的老的JavaScript代码:
function Deferred {
//私人数据
var isPending;
var handlers = {
resolve:[],
reject:[],
notify:[]
};
//初始化实例
init();
function init(){
isPending = true;
this.promise = new Promise(this);
}
//公共方法
this.resolve = function(value){
trigger('resolve',value);
};
this.reject = function(reason){
trigger('reject',reason);
};
this.notify = function(value){
trigger('notify',value);
};
this.on = function(event,handler){
...
};
//私有方法
函数触发器(事件,参数){
...
}
}
解决方案正确。
class
语法用于带有原型方法的普通类。如果你想要私有变量,你一直把它们放在构造函数中:class Deferred {
constructor
//初始化私有数据
var isPending = true;
var handlers = {
resolve:[],
reject:[],
notify:[]
};
//私有方法
函数触发器(事件,参数){
...
}
//初始化公共属性
this.promise = new Promise(this);
//并创建特权方法
this.resolve = trigger.bind(null,'resolve');
this.reject = trigger.bind(null,'reject');
this.notify = trigger.bind(null,'notify');
this.on = function(event,handler){
...
};
}
}
I'm just trying out ES6 and want to rewrite a part of code written in regular javascript to ES6. And now, I'm stuck while trying to re-write the private properties and methods in ES6 classes. It seems like classes in ES6 doesn't explicitly provide anything to have private data or methods.
Also, I checked this thread: Private properties in JavaScript ES6 classes and found out that we could use WeakMap to store private data. Which is sort of weird but still it can be a work around. And I did manage to use it for private data.
But what about private methods? What is the recommended way to have private methods (or even protected methods) in ES6 classes?
I would appreciate if anyone can show me a clean way to rewrite this part of code using ES6 class along with the private methods.
Thanks.
Here's the plain old javascript code:
function Deferred() { // Private data var isPending; var handlers = { resolve: [], reject: [], notify: [] }; // Initialize the instance init(); function init() { isPending = true; this.promise = new Promise(this); } // Public methods this.resolve = function(value) { trigger('resolve', value); }; this.reject = function(reason) { trigger('reject', reason); }; this.notify = function(value) { trigger('notify', value); }; this.on = function(event, handler) { ... }; // Private method function trigger (event, params) { ... } }
解决方案Correct. The
class
syntax is for normal classes with prototype methods. If you want private variables, you put them in the constructor as always:class Deferred { constructor() { // initialise private data var isPending = true; var handlers = { resolve: [], reject: [], notify: [] }; // Private method function trigger(event, params) { ... } // initialise public properties this.promise = new Promise(this); // and create privileged methods this.resolve = trigger.bind(null, 'resolve'); this.reject = trigger.bind(null, 'reject'); this.notify = trigger.bind(null, 'notify'); this.on = function(event, handler) { … }; } }
这篇关于有私人财产ES6类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!