简体   繁体   English

如何从 javascript 内置的 class 模块中导出 function?

[英]How do I export a function from a module for a class that is built into javascript?

I'm trying to export a function to use in another module, but it's one that I'm adding to the String javascript class.我正在尝试导出一个 function 以在另一个模块中使用,但它是我添加到字符串 javascript class 中的一个。

How would I export the function to use in another module?我将如何导出 function 以在另一个模块中使用?

This is what I tried and it didn't work这是我尝试过的方法,但没有用

String.prototype.trimChars = function(chars) {...}
export {String}

How do I make it so that this function is added to the string class as trimChars?如何将此 function 作为 trimChars 添加到字符串 class?

You can simply forgo the export, and import in another module.您可以简单地放弃导出,并在另一个模块中导入。 However, patching objects like this is not recommended, as it can introduce problems, if say, a browser adds a new function to String that happens to have the same name.但是,不建议像这样修补对象,因为它会引入问题,例如,浏览器将新的 function 添加到恰好具有相同名称的String

// in the other module
import "path/to/module.js";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM