Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中扩展String.prototype_Javascript_Visual Studio Code_Intellisense - Fatal编程技术网

在JavaScript中扩展String.prototype

在JavaScript中扩展String.prototype,javascript,visual-studio-code,intellisense,Javascript,Visual Studio Code,Intellisense,我已经开始将字符串相关函数作为JavaScript中的扩展方法 我的问题是,当我添加一个如下所示的扩展方法时,该方法可以工作,但在VS代码的Intellisense中没有显示 String.prototype.format = function() { let str = this.toString() if (arguments.length) { const type = typeof arguments[0] const args = typ

我已经开始将字符串相关函数作为JavaScript中的扩展方法

我的问题是,当我添加一个如下所示的扩展方法时,该方法可以工作,但在VS代码的Intellisense中没有显示

String.prototype.format = function() {
    let str = this.toString()
    if (arguments.length) {
        const type = typeof arguments[0]
        const args = type === 'string' || type === 'number' ? Array.prototype.slice.call(arguments) : arguments[0]

        for (const arg in args) str = str.replace(new RegExp(`\\{${arg}\\}`, 'gi'), args[arg])
    }
    return str
}

例如,如果我在.js文件中定义扩展名,然后导入该文件,我希望看到类似“test.format”的方法,但我没有看到该方法

创建一个包含函数类型声明的文件
index.d.ts

interface String {
    format(): string;
}

有关详细信息,请参见旁注中的

,通常不建议修改您不拥有的原型,请参见。您是否尝试在Type Declaration.d.ts文件中添加格式作为字符串方法?这是用于Typescript的。我想要一种纯JS的方式。不,在VisualStudio代码中,它也是用于javascript的。从文档中:
因为JavaScript和TypeScript现在基于相同的语言服务,所以它们能够以更丰富的方式进行交互。例如,可以为.d.ts文件
中声明的值提供JavaScript IntelliSense,用于可以使用JSDoc的简单函数。但是对于更复杂的东西,比如您的示例JSDoc,工作不太好。