鍍金池/ 問答/HTML/ test().a('I'm A.').b('I'm B')

test().a('I'm A.').b('I'm B')

如題,想要使用test().a('I'm A.').b('I'm B'),輸出test a I'm A b I'm B 該如何定義test?

回答
編輯回答
焚音
   function test() {
        function a(){
            console.log(" a I'm A ")
            return this
        }
        function b(){
            console.log("b I'm B")
            return this
        }
        console.log('test')
        return {
            a: a,
            b: b
        }
    }
    test().a().b()

jquery 是這方面的老大哥,你可以參考下 源碼,不過這個夠你 目前 用了
樓下 回答才是正解。。。

2018年7月6日 19:21
編輯回答
呆萌傻
function test () {
  return {
    callInfo: 'test',
    a (arg) {
      this.callInfo += ' ' + arguments.callee.name + ' ' + arg
      return this
    },
    b (arg) {
      return (this.callInfo += ' ' + arguments.callee.name + ' ' + arg)
    }
  }
}
test().a('I\'m A.').b('I\'m B')

2017年12月13日 18:51