变量范围:跨类函数调用

时间:2010-09-21 20:48:31

标签: javascript jquery

我有一个vehicle和一个product对象,我需要车辆来调用产品中的功能....我似乎无法弄明白,我该怎么办?< / p>

var vehicle = function () {
    return {
        init: function () {
            var that = this;

            jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
                e.preventDefault();
                that.remove(jQuery(e.currentTarget).parents('.vehicle-year-profile'));
            });

            jQuery('.vehicle-year-profile .options .edit').bind('click', function (e) {
                e.preventDefault();
                that.edit(jQuery(e.currentTarget).parents('.vehicle-year-profile').attr('id'));
            });

            jQuery('#association-detail .save').bind('click', function (e) {
                e.preventDefault();
                that.save();
            });
        },
        edit: function (id) {},
        save: function () {},
        remove: function (el) {},
        reset: function () {}
    }
}();

var product = function () {
    return {
        refreshHistory: function () {}
    };
}();

1 个答案:

答案 0 :(得分:2)

你试过吗

product.refreshHistory();

??变量“product”是全局的(或至少相对全局),因此“vehicle”对象中的代码可以直接引用它。