TypeError:this.name不是函数

时间:2016-11-13 19:38:37

标签: javascript typescript

在下面的代码中,我总是收到错误“TypeError:this.verifyUrl不是Server.ImageServer.handleRequest中的函数”,即使上面定义了该函数。

任何暗示欢迎。

如果我用纯JavaScript编写样本,同样适用。

"use strict";

import Http = require('http');
import Url = require('url');

export class ImageServer {

    port: number;
    server: Http.Server;

    constructor(port: number) {
        this.port = port || 1337;
    }

    run() {
        this.server = Http.createServer(this.handleRequest);
        this.server.listen(this.port);
    }

    verifyUrl(urlitems: Url.Url) {
        return true;
    }

    handleRequest(req: Http.IncomingMessage, res: Http.ServerResponse) {
        console.log('request: ', req.url);
        var urlitems = Url.parse(req.url, true);
        var pathitems = urlitems.path.split('/').slice(1);

        console.log('url: ', urlitems);
        console.log('path: ', pathitems);

        if (!this.verifyUrl(urlitems)) {
            this.sendNotFound(res);
            return;
        }

        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Hello World\n');
    }

    sendNotFound(res: Http.ServerResponse) {
        res.statusCode = 404;
        res.end();
        return undefined;
    }

}

2 个答案:

答案 0 :(得分:1)

试试这个:

"use strict";

import Http = require('http');
import Url = require('url');

export class ImageServer {

    port: number;
    server: Http.Server;

    constructor(port: number) {
        this.port = port || 1337;
    }

    run() {
        this.server = Http.createServer(this.handleRequest);
        this.server.listen(this.port);
    }

    verifyUrl(urlitems: Url.Url) {
        return true;
    }

    handleRequest = (req: Http.IncomingMessage, res: Http.ServerResponse) => {
        console.log('request: ', req.url);
        var urlitems = Url.parse(req.url, true);
        var pathitems = urlitems.path.split('/').slice(1);

        console.log('url: ', urlitems);
        console.log('path: ', pathitems);

        if (!this.verifyUrl(urlitems)) {
            this.sendNotFound(res);
            return;
        }

        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Hello World\n');
    }

    sendNotFound(res: Http.ServerResponse) {
        res.statusCode = 404;
        res.end();
        return undefined;
    }

}

我想你将handleRequest称为来自模块的回调(可能是expressjs?),因此this没有绑定到类范围,而是绑定到模块的范围。使用箭头函数,this将自动分配给类范围,因此您可以访问类函数/属性

这是用例子解释的问题

class Hi {
   hello: string = "Hello world!";

   haha(req: Http.IncomingMessage, res: Http.ServerResponse) {
      console.log(this.hello) // print undefined or error
   }
}

var hi = new Hi();

app.get("/foo", hi.haha);

使用此代码,您无法访问类属性,因为this已分配表达。

您可以使用箭头功能修复上述内容。这是一个有效的例子:

class Hi {
   hello: string = "Hello world!";

   haha = (req: Http.IncomingMessage, res: Http.ServerResponse) => {
      console.log(this.hello) // print Hello world!
   }
}

var hi = new Hi();

app.get("/foo", hi.haha);

答案 1 :(得分:0)

快速浏览它看起来像是一个范围问题,因为在调用时this指的是Http.createServer()的范围。快速解决方法是绑定到实际的对象范围imo。