f1n*_*1nn 12 inheritance scope super ecmascript-6 es6-promise
我试图save()
从子实例调用super方法.
// ImageKeeper.js
'use strict';
module.exports = class ImageKeeper extends FileKeeper {
constructor(keeperPath, options) {
super(`/${keeperPath}`, options)
this.keeperPath = keeperPath;
}
save(filePath) {
return new Promise((resolve, reject) => {
this
.resolvePath(filePath)
.then(function(fileData) {
var filePath = fileData[0],
mime = fileData[1];
super.save(filePath, mime); // <-- GETTING ERROR HERE
})
.catch(function(err) {
reject(err)
})
})
}
}
// FileKeeper.js
'use strict';
module.exports = class FileKeeper {
constructor(keeperPath, options) {
this.storagePath = path.resolve(`${env.paths.storage}${keeperPath}`);
this.options = options;
}
save(filePath, mime) {
return Promise
...
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
/src/filekeeper/imagekeeper.js:110
super.save(filePath, mime);
^^^^^
SyntaxError: 'super' keyword unexpected here
Run Code Online (Sandbox Code Playgroud)
如果我super.save(filePath, mime);
在save()
方法的开头移动,它可以工作.
我已经尝试将绑定上下文绑定到上限范围:
save(filePath) {
return new Promise((resolve, reject) => {
this
.then((fileData) => { // <-- bind context to upper scope
super.save(filePath, mime);
Run Code Online (Sandbox Code Playgroud)
但是我得到了:
Unhandled rejection SyntaxError: 'super' keyword unexpected here
at processImmediate [as _immediateCallback] (timers.js:374:17)
From previous event:
at /src/filekeeper/imagekeeper.js:106:10
Run Code Online (Sandbox Code Playgroud)
读这个,但没有运气.
有任何想法吗?谢谢.
root@8d1024b233c3:/src# node -v
v4.1.1
docker -v
Docker version 1.8.2, build 0a8c2e3
Run Code Online (Sandbox Code Playgroud)
看起来你在V8的处理中发现了一个错误super
; 我已经在这里报告了这个错误,他们已经将它分类为Type-Bug
和Priority-Medium
.这是在仔细研究之后,导致我发布这个问题,这个答案证实了我怀疑这是一个V8错误.
如果您使用箭头函数(不是function
函数)按照"我已经尝试将绑定上下文绑定到上部范围"注释(主代码块使用的function
函数不起作用),它应该正常工作.
在等待修复时,如果您将该逻辑放在方法中,它会起作用:
someAppropriateName(fileData) {
var filePath = fileData[0],
mime = fileData[1];
super.save(filePath, mime);
}
Run Code Online (Sandbox Code Playgroud)
...并从promise回调中调用该方法:
save(filePath) {
return new Promise((resolve, reject) => {
this
.resolvePath(filePath)
.then(fileData => { // **
this.someAppropriateName(fileData); // **
}) // **
.catch(function(err) {
reject(err)
})
})
}
Run Code Online (Sandbox Code Playgroud)
要么:
save(filePath) {
return new Promise((resolve, reject) => {
this
.resolvePath(filePath)
.then(this.someAppropriateName.bind(this)) // **
.catch(function(err) {
reject(err)
})
})
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为错误是相当模糊的:只有在方法内的另一个箭头函数内有箭头函数时,它才会出现,而最里面的箭头函数使用由外部箭头函数定义的变量或参数(使用方法本身的东西)很好).
其他一些说明:
如果FileKeeper
的save
返回一个承诺,似乎ImageKeeper
应该使用和链接关闭它.你的代码只是抛弃了调用的结果super.save(...)
.
当你发现自己写作时new Promise
,总是停下来问自己,所讨论的代码是否真的是链的根源.非常,非常,经常不是(我怀疑它不在您的代码中).请记住,每个人都会then
回报承诺,承诺的力量主要在于链条.如果你不需要,不要打破链条.
归档时间: |
|
查看次数: |
4316 次 |
最近记录: |