最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extends an object with class in ES6? - Stack Overflow

programmeradmin3浏览0评论

I am trying to cleaning my ES6 class definition, I have this kind of code now:

class SomeClass {
  constructor({a, b, c, d, e}) {
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
    this.e = e;
    // some codes here
  }
  // some methods here..
}

This code extends everything passing to the constructor.

I use it in this way:

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5});

When I want to change the parameter passing to the constructor, such as:

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5, f:6});

I want obj.f to be 6.

I want the constructor to be reusable if I am passing different params to it. If the params passing to the constructor changes, the return value changes.

I tried this:

class SomeClass extends Object {
  constructor(params) {
    super(params);
    // some codes here
  }
  // some methods here..
}

but this does not work.

So why this does not work? If I extends the Object class, why super({a:1}) does not return {a:1}?

$ node
> new Object({a:1})
{ a: 1 }
> class MyClass extends Object {
... constructor(params) { super(params); }
... }
undefined
> new MyClass({a:1})
MyClass {}

And how to achieve my goal?

I am trying to cleaning my ES6 class definition, I have this kind of code now:

class SomeClass {
  constructor({a, b, c, d, e}) {
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
    this.e = e;
    // some codes here
  }
  // some methods here..
}

This code extends everything passing to the constructor.

I use it in this way:

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5});

When I want to change the parameter passing to the constructor, such as:

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5, f:6});

I want obj.f to be 6.

I want the constructor to be reusable if I am passing different params to it. If the params passing to the constructor changes, the return value changes.

I tried this:

class SomeClass extends Object {
  constructor(params) {
    super(params);
    // some codes here
  }
  // some methods here..
}

but this does not work.

So why this does not work? If I extends the Object class, why super({a:1}) does not return {a:1}?

$ node
> new Object({a:1})
{ a: 1 }
> class MyClass extends Object {
... constructor(params) { super(params); }
... }
undefined
> new MyClass({a:1})
MyClass {}

And how to achieve my goal?

Share Improve this question edited Apr 2, 2019 at 11:22 cmal asked Apr 2, 2019 at 11:19 cmalcmal 2,2222 gold badges23 silver badges37 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

I'm not quite sure I understand your need, but it seems like somthing simple like this should suit your needs:

class SomeClass {
  constructor(obj) {
    Object.assign(this, obj);
    // some codes here
  }
  // some methods here..
}

That way

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5, f:6});

works just fine.

Think you probably want something like:

class SomeClass {
  constructor(o) {
    Object.assign(this, o);   
  }  
}

console.log(new SomeClass({a:1}))
console.log(new SomeClass({a:1, b:2, c:3, d:4, e:5}))

We can use spread operator to get rest of arguments.

Example :

class SomeClass {
  constructor({a, b, c, d, e, ...rest}) { 
    console.log(rest);
  }
}

here rest variable will have rest of all aruments can be n.

and you can with

var obj = new SomeClass({a:1, b:2, c:3, d:4, e:5, f:6, g: 7});

i hope this help you

As other people noted, using Object.assign would solve your case and not extending from Object.

class SomeClass {
  constructor(obj) {
    Object.assign(this, obj);   
  }  
}


console.log(new SomeClass({a:1, b:2, c:3, d:4, e:5, f:6}))

As for the answer, when you call super(params) in a class extended by a Object, you are calling the constructor of Object, which doesn't have parameters.

When you use new Object({a:1}) you are calling a method and not a constructor.

It was confusing to me at first too and I had to research for it.

Reference and more info here:

https://www.ecma-international/ecma-262/6.0/#sec-properties-of-the-object-constructor

发布评论

评论列表(0)

  1. 暂无评论