ES6 & Classes & Interface
ES6 & Classes & Interface
what's the difference between javascript Classes & Interface ?
https://github.com/microsoft/TypeScript/issues/31788
very confused codes, which Point
is class & which Point
is Interface
Point.prototype.distanceFromOrigin = function (this: Point, point: Point)
http://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#no-implicit-any-for-this
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019.06.06
*
* @description
* @augments
* @example
* @link
*
*/
class Point {
constructor(public x, public y) {
// ...
}
getDistance(p: Point) {
let dx = p.x - this.x;
let dy = p.y - this.y;
return Math.sqrt(dx ** 2 + dy ** 2);
}
}
// Reopen the interface.
interface Point {
distanceFromOrigin(point: Point): number;
}
// Point.prototype.distanceFromOrigin = function (point: Point) {
// return this.getDistance({
// x: 0,
// y: 0,
// });
// }
// ???
Point.prototype.distanceFromOrigin = function (this: Point, point: Point) {
return this.getDistance({
x: 0,
y: 0,
});
}
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10985783.html
未经授权禁止转载,违者必究!