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,
    });
}



ES6 & Classes & Interface

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


相关文章:

  • 2022-12-23
  • 2022-02-23
  • 2021-08-31
  • 2021-06-20
  • 2021-11-21
  • 2021-12-02
  • 2021-08-08
  • 2021-10-13
猜你喜欢
  • 2021-06-07
  • 2021-08-26
  • 2021-10-28
  • 2021-12-08
  • 2022-03-07
  • 2021-08-04
  • 2021-06-24
相关资源
相似解决方案