How do I write an extension method in JavaScript?

后端 未结 2 1339
鱼传尺愫
鱼传尺愫 2020-11-28 02:45

I need to write a few extension methods in JS. I know just how to do this in C#. Example:

public static string SayHi(this Object name)
{
    return \"Hi \"         


        
2条回答
  •  不知归路
    2020-11-28 03:27

    Using Global augmentation

    declare global {
        interface DOMRect {
            contains(x:number,y:number): boolean;
        }
    }
    /* Adds contain method to class DOMRect */
    DOMRect.prototype.contains = function (x:number, y:number) {                    
        if (x >= this.left && x <= this.right &&
            y >= this.top && y <= this.bottom) {
            return true
        }
        return false
    };
    

提交回复
热议问题