阅读:10202 次 编辑日期:2014-05-05
function auto(){} auto.prototype = { setName:function(name){ this.name = name; }, setAge:function(age){ this.age = age; } } var setFun = new auto(); alert(JSON.stringify(setFun));//{} setFun.setName("uw3c");//单个调用 setFun.setAge(1);//单个调用 alert(JSON.stringify(setFun));//{"name":"uw3c","age":1}
function auto(){} auto.prototype = { setName:function(name){ this.name = name; return this; }, setAge:function(age){ this.age = age; return this; } } var setFun = new auto(); alert(JSON.stringify(setFun));//{} setFun.setName("uw3c").setAge(1);//链式调用 alert(JSON.stringify(setFun));//{"name":"uw3c","age":1}
setFun.setName("uw3c") == this;//因为setName方法返回this,这里this == setFun。 setFun.setName("uw3c").setAge(1) == this.setAge(1) == setFun.setAge(1);