我想从同一个对象中的另一个方法调用一个方法

时间:2018-11-11 17:29:59

标签: javascript node.js

我有一个物体。 但是,我不能称其为方法-方法 start 中的 cancelAdd node.js写道:“ cancelAdd不能正常工作。

https://prnt.sc/lh16tj

https://jsbin.com/yubezikija/edit?js,output

请帮帮我。 对不起,英语不好,我正在学习。

const config = require('../ config / config');

    int main(int argc, char const *argv[]) { 
    struct sockaddr_in address; 
    int sock = 0, valread; 
    struct sockaddr_in serv_addr; 
    // char *hello = "Hel0o from client"; 
    char *hello; 
    char buffer[1024] = {0}; 

    std::string msg;

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 
        printf("\n Socket creation error \n"); 
        return -1; 
    } 

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_port = htons(PORT); 

    // Convert IPv4 and IPv6 addresses from text to binary form 
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)  { 
        printf("\nInvalid address/ Address not supported \n"); 
        return -1; 
    } 

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { 
        printf("\nConnection Failed \n"); 
        return -1; 
    }

    while (true) {
        std::cout << "msg: ";
        std::cin >> msg;

        hello = new char[msg.length() + 1];
        std::strcpy(hello, msg.c_str());

        send(sock , hello , strlen(hello) , 0 );
    }

    printf("Hello message sent\n"); 
    valread = read( sock , buffer, 1024); 
    printf("%s\n",buffer ); 
    return 0; 
}

我在文件app.js中调用方法 start 。 在此代码下方。

const dictionaryOperations = (bot) =>{
    return {
        start: function (msg) {
            let chatId = msg.chat.id;
            let message = 'Welcome!\nThis bot can save words as dictionary';
            let options = {
              "reply_markup": {
                  "keyboard": [[config.replyKeyboard.addWordButton]],
                  "resize_keyboard": true,
              }
            };
            bot.sendMessage(chatId, message, options);
            this.cancelAdd(msg);
        },
        cancelAdd: function (msg) {
            console.log( `cancel action`);
            bot.send(msg.chat.id, 'Cancel action');
        },
    }

};

module.exports = dictionaryOperations;

0 个答案:

没有答案