如何使用 ES6 class 实例属性和方法扩展对象
How to extend object with ES6 class instance properties and methods
我正在重构旧 ES5 代码库中的一些代码,我在其中执行以下操作:
function ObjectCreatorFunction() {
this.someProperty= {};
}
/*
* Static method, to extend the object passed as a parameter with the
* ObjectCreatorFunction instance properties and methods
*/
ObjectCreatorFunction.extend = function extend(object) {
var key;
ObjectCreatorFunction.call(object);
for (key in ObjectCreatorFunction.prototype) {
if (ObjectCreatorFunction.prototype.hasOwnProperty(key)) {
if (!object.hasOwnProperty(key)) {
object[key] = ObjectCreatorFunction.prototype[key];
}
}
}
return object;
};
ObjectCreatorFunction.prototype.someMethod = function someMethod() {...}
//etc
我正在尝试对 ES6 重写做同样的事情,所以我有这个
class ClassName{
constructor() {
this.someProperty= {};
}
static extend(object) {
let key;
ClassName.constructor.call(object);
for (key in ClassName.prototype) {
if (ClassName.prototype.hasOwnProperty(key)) {
if (!object.hasOwnProperty(key)) {
object[key] = ClassName.prototype[key];
}
}
}
return object;
}
someMethod() {...}
//etc
}
我的问题是线路
ClassName.constructor.call(object);
没有按预期工作,即传递的对象没有获得 class 的实例属性。
我已经尝试了几种方法来重写它(甚至一些非正统的)都无济于事。
如何使用 ES6 扩展具有 class' 实例属性的对象?
免责声明:
我的代码是通过 babel 和 webpack 的转译过程传递的。以防它对 classes 的内部工作方式有任何影响。
不,这不适用于 class
语法。 不仅仅是语法糖。原型继承保持不变,但现在实例的初始化工作方式不同,特别是对于继承 类,并且您不能在没有 new
的情况下调用构造函数来不创建新实例。
我建议明确说明你的 mixin,并给它一个 init
方法:
class Mixin {
constructor(methods) {
this.descriptors = Object.getOwnPropertyDescriptors(methods);
}
extend(object) {
for (const p in this.descriptors)) {
if (Object.prototype.hasOwnProperty.call(object, p)) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`Object already has property "${p}"`);
}
} else {
Object.defineProperty(object, p, this.descriptors[p]);
}
}
}
}
// define a mixin:
const xy = new Mixin({
initXY() {
this.someProperty= {};
},
someMethod() { … }
});
// and use it:
class ClassName {
constructor() {
this.initXY();
}
}
xy.extend(ClassName.prototype);
我正在重构旧 ES5 代码库中的一些代码,我在其中执行以下操作:
function ObjectCreatorFunction() {
this.someProperty= {};
}
/*
* Static method, to extend the object passed as a parameter with the
* ObjectCreatorFunction instance properties and methods
*/
ObjectCreatorFunction.extend = function extend(object) {
var key;
ObjectCreatorFunction.call(object);
for (key in ObjectCreatorFunction.prototype) {
if (ObjectCreatorFunction.prototype.hasOwnProperty(key)) {
if (!object.hasOwnProperty(key)) {
object[key] = ObjectCreatorFunction.prototype[key];
}
}
}
return object;
};
ObjectCreatorFunction.prototype.someMethod = function someMethod() {...}
//etc
我正在尝试对 ES6 重写做同样的事情,所以我有这个
class ClassName{
constructor() {
this.someProperty= {};
}
static extend(object) {
let key;
ClassName.constructor.call(object);
for (key in ClassName.prototype) {
if (ClassName.prototype.hasOwnProperty(key)) {
if (!object.hasOwnProperty(key)) {
object[key] = ClassName.prototype[key];
}
}
}
return object;
}
someMethod() {...}
//etc
}
我的问题是线路
ClassName.constructor.call(object);
没有按预期工作,即传递的对象没有获得 class 的实例属性。
我已经尝试了几种方法来重写它(甚至一些非正统的)都无济于事。
如何使用 ES6 扩展具有 class' 实例属性的对象?
免责声明:
我的代码是通过 babel 和 webpack 的转译过程传递的。以防它对 classes 的内部工作方式有任何影响。
不,这不适用于 class
语法。 new
的情况下调用构造函数来不创建新实例。
我建议明确说明你的 mixin,并给它一个 init
方法:
class Mixin {
constructor(methods) {
this.descriptors = Object.getOwnPropertyDescriptors(methods);
}
extend(object) {
for (const p in this.descriptors)) {
if (Object.prototype.hasOwnProperty.call(object, p)) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`Object already has property "${p}"`);
}
} else {
Object.defineProperty(object, p, this.descriptors[p]);
}
}
}
}
// define a mixin:
const xy = new Mixin({
initXY() {
this.someProperty= {};
},
someMethod() { … }
});
// and use it:
class ClassName {
constructor() {
this.initXY();
}
}
xy.extend(ClassName.prototype);