Merge two functions?

后端 未结 5 1183
礼貌的吻别
礼貌的吻别 2021-01-24 04:32

Say, I have two functions:

function foo() {
  this.lorem = \'ipsum\';
}

function boo() {
  console.log(this.lorem);
}

And I want to insert the

5条回答
  •  花落未央
    2021-01-24 04:40

    Wrap them both under the same context:

    var myClass = {
        foo: function foo() {
            this.lorem = 'ipsum';
            this.boo();
        }, boo: function boo() {
            alert(this.lorem);
        }
    };
    

    Then to activate foo:

    myClass.foo();
    

    Live test case.

提交回复
热议问题