合并两个功能? [英] Merge two functions?

查看:80
本文介绍了合并两个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有两个功能:

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

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

我想插入 boo 函数在 foo 函数的末尾,以便它们可以共享相同的。我该怎么办?

And I want to insert the boo function at the end of the foo function so that they can share the same this. How would I do it?

推荐答案

在相同的上下文中包装它们:

Wrap them both under the same context:

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

然后激活 foo

myClass.foo();

实时测试案例

这篇关于合并两个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆