<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>链式调用</title> </head> <body> <script type="text/javascript"> window.com=window.com || {}; com.memorycat=com.memorycat || {}; com.memorycat.Person=function(name){ //一些私有成员。。。。 var personName=name; return { say:function (str){ console.info(personName+' say:'+str); return this;//关键 }, eat:function (food){ console.info(personName+' eat:'+food); return this;//关键 }, driver:function(car){ console.info(personName+' driver:'+car); return this;//关键 } } }; var man = new com.memorycat.Person("李四") ; console.info(man) man.say('usb').eat('shit').driver('bike'); </script> </body> </html>
输出结果
李四 say:usb 李四 eat:shit 李四 driver:bike