使用3种方法创建JavaScript对象throws Object没有方法

Pho*_*_uy 1 javascript object

我试图创建一个名为JavaScript对象human有三种方法walk,eat并且talk,我想这样称呼它(没有方法应该打印任何值)human.talk('hello').walk('home').eat('pizza').

我有这个代码:

var human = {
    talk : function talk(t){

    },
    walk : function walk(w){

    },
    eat : function eat(e){

    }
};

console.log(human.talk('hello').walk('home').eat('pizza'));
Run Code Online (Sandbox Code Playgroud)

但我收到了 Uncaught TypeError: Cannot call method 'walk' of undefined

为什么??

Rob*_*xyz 6

return this如果您希望能够链接函数,则每个函数都需要.您收到错误,因为函数talk正在返回undefined,您实际上是在尝试调用undefined.walk('home').