我试图创建一个名为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
为什么??
return this如果您希望能够链接函数,则每个函数都需要.您收到错误,因为函数talk正在返回undefined,您实际上是在尝试调用undefined.walk('home').
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |