どのようにES6でバインドを使用できますか?コーディングの前ES6のようにJavascript.bind - ES5対ES6
(私はこれがES5であると仮定)、私はどうなる:
var app = {};
app.log = function(req, res) {
var respond = this.respond.bind(this, req, res);
return respond(400, 'no data received');
}
app.respond = function(req, res, status, message) {
console.log(req); // hello
console.log(status); // 400
console.log(message); // no data received
}
app.log('hello');
しかし、私はES6でそれを行うことができますか?もちろん
export default function log (req, res) {
var respond = this.respond.bind(this, req, res);
return respond(400, 'no data received');
}
function respond (req, res, status, message) {
console.log(req);
console.log(status);
console.log(message);
}
私はエラーが発生します。
TypeError: Cannot read property 'respond' of undefined
:
あなたがしなければならないのは、関数を呼び出すのですか? – Bergi
WTHあなたは 'bind'を全く使っていますか?ちょうど 'this.respond(req、res、400、 'no data received');'(それと同等のES6ソリューションはそれ自体を提示します) – Bergi
'bind'がどのように動作するか見てみたいと思います。他のプログラムで使用されているので、参照として使用する必要があります。そのため、なぜバインドを使用するのか理解しようとしています。 – laukok