Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Classes: Compose a Triangle from extending 3 points?

Question: How can I take a Triangle Class extend Point(supers(?)) and compose an object that looks like this:

//  "name":"Thomas The Triangle",
//  "points": [
//     {age: "2015-05-28T06:23:26.160Z", x: 1, y: 1 }, 
//     {age: "2015-05-28T06:23:26.161Z", x: 0, y: 3 },
//     {age: "2015-05-28T06:23:26.164Z", x: 2, y: 3 }
//  ]

JS:

class Point {
  constructor(x, y){
    this.name = "Point"
    this.age = new Date();
    this.x = x;
    this.y = y;
  }
}



class Triangle extends Point{
  constructor(coords, name) {

    super(coords[0][0], coords[0][1]); //this line is best I could do but not correct
    this.name = name
  }


}
let t1 = new Triangle([[1,1],[0,3],[2,3]], 'Thomas The Triangle')
console.log(t1);

**

Live code for ES6

**

like image 833
Armeen Harwood Avatar asked May 28 '15 06:05

Armeen Harwood


1 Answers

It doesn't look like Triangle needs to extend Point. It rather compose multiple Point objects into array of points. Something like this:

class Triangle {
  constructor(coords, name) {
    this.points = coords.map((point) => {
        return new Point(...point);                        
    });
    this.name = name;
  }
}
like image 126
dfsq Avatar answered Nov 15 '22 06:11

dfsq