Class 来自 CommonJS 模块的方法无法访问?
Class method from CommonJS module not accessible?
我正在研究 Chapter 11 of the Eloquent Javascript book. The book provides a CommonJS module for the code related to the chapter: crow-tech.js
练习 "Tracking the Scalpel" 的解决方案
以下是我目前的解决方案代码:
const ct = require('./crow-tech');
function storage(nest, name) {
return new Promise(resolve => {
nest.readStorage(name, result => resolve(result));
});
}
async function locateScalpel(nest) {
let place = await storage(nest, 'scalpel');
if (place === nest.name) {
return place;
} else if (place !== null) {
return await locateScalpel(place);
} else {
return null;
}
}
function locateScalpel2(nest) {
// Your code here.
}
locateScalpel(ct.bigOak).then(console.log);
// → Butcher Shop
这里 ct.bigOak
是 class Node
的对象,其中包含方法 readStorage
。在使用 console.log
进行的独立测试中,我可以看到 ct.bigOak
已正确导入并且 ct.bigOak.readStorage
是一个函数。但是,当我在 Node 中 运行 上述代码时,我收到此错误消息:
(node:5441) UnhandledPromiseRejectionWarning: TypeError: nest.readStorage is not a function
at resolve (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:5:10)
at new Promise (<anonymous>)
at storage (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:4:10)
at locateScalpel (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:10:23)
at locateScalpel (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:14:22)
(node:5441) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:5441) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将 ct.bigOak
传递到我的本地函数中是否存在一些问题导致 readStorage
方法无法被识别?
问题出在对 locateScalpel 的第二次调用,而不是第一次。
locateScalpel(place)
- 这里。
我正在研究 Chapter 11 of the Eloquent Javascript book. The book provides a CommonJS module for the code related to the chapter: crow-tech.js
练习 "Tracking the Scalpel" 的解决方案以下是我目前的解决方案代码:
const ct = require('./crow-tech');
function storage(nest, name) {
return new Promise(resolve => {
nest.readStorage(name, result => resolve(result));
});
}
async function locateScalpel(nest) {
let place = await storage(nest, 'scalpel');
if (place === nest.name) {
return place;
} else if (place !== null) {
return await locateScalpel(place);
} else {
return null;
}
}
function locateScalpel2(nest) {
// Your code here.
}
locateScalpel(ct.bigOak).then(console.log);
// → Butcher Shop
这里 ct.bigOak
是 class Node
的对象,其中包含方法 readStorage
。在使用 console.log
进行的独立测试中,我可以看到 ct.bigOak
已正确导入并且 ct.bigOak.readStorage
是一个函数。但是,当我在 Node 中 运行 上述代码时,我收到此错误消息:
(node:5441) UnhandledPromiseRejectionWarning: TypeError: nest.readStorage is not a function
at resolve (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:5:10)
at new Promise (<anonymous>)
at storage (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:4:10)
at locateScalpel (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:10:23)
at locateScalpel (/home/<username>/programming/js/eloquent-javascript/tracking-the-scalpel.js:14:22)
(node:5441) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:5441) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将 ct.bigOak
传递到我的本地函数中是否存在一些问题导致 readStorage
方法无法被识别?
问题出在对 locateScalpel 的第二次调用,而不是第一次。
locateScalpel(place)
- 这里。