小编Bre*_*nan的帖子

为什么我的ES6(使用Babel)类在实例方法中未定义`this`?

我正在使用Hapi.JS在Node中构建应用程序.

我有一个认证插件的类,它给了我各种各样的问题.当我尝试this从类中的方法引用时,我得到一个错误,说明this是未定义的.为什么会这样?

摘录:

class OAuth {

  constructor () {}

  register (server, err, next) {
    this.server = server;
    this.registerRoutes();
  }

  registerRoutes () {
    console.log(this.server.route);
    this.server.route([
      {
          method: 'POST',
          path: '/oauth/token',
          config: {
              auth: false,
              handler: function(request,reply){
                console.log("test");
                reply("test");
              }
            }
      },
      {
        method: 'GET',
        path: '/test',
        config: {
          auth: false,
          handler: function(request,reply){
            console.log("test");
            reply("test");
          }
        }
      }
    ]);
  }
}
module.exports = new OAuth();
Run Code Online (Sandbox Code Playgroud)

在其他地方,这被称为:

const oauth = require('./oauth');
oauth.register(server);
Run Code Online (Sandbox Code Playgroud)

每次调用寄存器函数时,都会收到此错误:

TypeError: Cannot set property 'server' of …
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-6 hapijs babeljs

11
推荐指数
1
解决办法
3276
查看次数

比较Ruby中的字节

我有一个JPG或MP4文件的二进制blob头.我试图区分这两者.

当文件是JPG时,前两个字节是\xFF\xD8.但是,当我进行比较时blob[0] == "\xFF",它失败了.即使我知道blob[0]IS实际上也是如此\xFF

做这个的最好方式是什么?

ruby binary image-processing

2
推荐指数
1
解决办法
945
查看次数