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




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM