如何将以下代码转换为ES6类:
var Mergesort = (function() {
function Mergesort(){}
Object.define(Mergesort.prototype,'readOnlyMethod',{
value:function(){/*DO SOMETHING, RETURN SOME VAL*/},
configurable:false,
writable:false,
enumerable:false
});
return Mergesort;
})()
答案 0 :(得分:1)
原样的ES6翻译是
const Mergesort = (() => {
class Mergesort {}
Object.defineProperty(Mergesort.prototype, 'readOnlyMethod', {
value() { /*DO SOMETHING, RETURN SOME VAL*/ },
configurable: false,
writable: false,
enumerable: false
});
return Mergesort;
})();
,但是假设您想将方法定义为class
表达式的一部分,那么之后只需更改属性描述符即可:
class Mergesort {
readOnlyMethod() { /*DO SOMETHING, RETURN SOME VAL*/ }
}
Object.defineProperty(Mergesort.prototype, 'readOnlyMethod', {configurable:false, writable:false});