You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example add can be called with a plain object that has an x and y properties that are numbers.
with add(that: Point2D) the method must be called with a Point2D or an object that has x and y as number and all the methods in Point2D class and you're forced to call add(new Point(1,1)) instead of add({x: 1, y: 1}).
A practical use-case with object destructuring:
const el = document.querySelector('#the-div')
const myPoint = new Point()
el.addEventListener('mousemove', ({clientX: x = 0, clientY: y = 0}) => {
myPoint = myPoint.add({x, y})
})
The IVector2D can be a alias of ICoordinate2D this can allow to:
interface IVector2D extends ICoordinate2D {}
class Vector2D extends IVector2D {
...
}
// this with allow to use Point2D with a Vector
// I'm not sure about this, if can be considered correct.
Vector2D v = new Vector2D(1,2)
Point2D p = new Point2D(1,1)
Point2D p2 = p.add(v) // this doesn't rais any warnings/errors becaus Vector2D is subclass of Vector2D and has numeric properties x and y
The text was updated successfully, but these errors were encountered:
Declaring basic Interfaces with only properties can help passing plain object to method.
In the example
add
can be called with a plain object that has an x and y properties that are numbers.with
add(that: Point2D)
the method must be called with a Point2D or an object that has x and y as number and all the methods in Point2D class and you're forced to calladd(new Point(1,1))
instead ofadd({x: 1, y: 1})
.A practical use-case with object destructuring:
The
IVector2D
can be a alias ofICoordinate2D
this can allow to:The text was updated successfully, but these errors were encountered: