首页 > 解决方案 > 导出我自己的 Array 原型方法并将其用于不同的文件

问题描述

我知道如何导出函数、类和变量。但是,我找不到任何清楚地解释如何导出原型方法并在不同文件上调用它的来源。如果我有像下面这样的原型方法,我如何在另一个文件上使用它?

//Index.js
Array.prototype.firstItem = function() {
  return this[0]
}

export {firstItem} //I know this is wrong but how to do it in similar syntax?

//anotherFile.js
import {firstItem} from './Index.js'

const testArray = ["Thank", "You", "For", "Your", "Answer"]
console.log(testArray.firstItem())

谢谢你的回答

标签: javascriptecmascript-6moduleprototype

解决方案


推荐阅读