如何使私人功能可以訪問另一個文件。我有以下兩個文件,我試圖訪問File2.js中的msg()。我怎樣才能做到這一點無需在File1.js使私人功能可以訪問外部文件
File1.js
function test() {
}
test.prototype.first = function() {
msg("abc");
}
function msg(name) {
console.log("hello " + name);
}
module.exports = test;
File2.js
var test = require("./File1");
console.log(test.msg("xyz"))
我收到錯誤的任何變化
TypeError: undefined is not a function
您可以將'msg'函數放入'test'函數中,或者將其添加到'test'函數的原型中。如果您不想修改File1,js,那麼請嘗試調用'first'函數,而不是調用'msg'函數,因爲該函數已經在'test'函數的原型中,但要小心,因爲'first'函數不接受參數並具有硬編碼的「abc」。 – jmugz3
當某個文件可以在另一個文件中訪問時,它不是私人的 –