Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extends an object with class in ES6?

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?

like image 975
cmal Avatar asked Apr 02 '19 11:04

cmal


People also ask

Can an object extend a class?

Object does in fact not have an extends clause (and because the Object class is primordial and has special treatment in the spec, there's no extends Object implicitly present). Additionally, you may observe that the value of Object. class.

How do you extend a class in JavaScript?

The syntax to extend another class is: class Child extends Parent . Let's create class Rabbit that inherits from Animal : class Rabbit extends Animal { hide() { alert(`${this.name} hides!`); } } let rabbit = new Rabbit("White Rabbit"); rabbit. run(5); // White Rabbit runs with speed 5.

How do you extend an object in JavaScript?

The extends keyword can be used to extend the objects as well as classes in JavaScript. It is usually used to create a class which is child of another class. Syntax: class childclass extends parentclass {...}

How do I extend a class in TypeScript?

Just like object-oriented languages such as Java and C#, TypeScript classes can be extended to create new classes with inheritance, using the keyword extends . In the above example, the Employee class extends the Person class using extends keyword.


3 Answers

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.

like image 138
Marius Avatar answered Oct 17 '22 00:10

Marius


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}))
like image 30
charlietfl Avatar answered Oct 16 '22 22:10

charlietfl


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.org/ecma-262/6.0/#sec-properties-of-the-object-constructor

like image 1
Daniel Piñeiro Avatar answered Oct 16 '22 22:10

Daniel Piñeiro