Javascript:混合构造函数模式和显示模块模式 [英] Javascript: Mixing constructor pattern and Revealing Module Pattern

查看:101
本文介绍了Javascript:混合构造函数模式和显示模块模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以让Javascript类扩展通过显示模块模式创建的对象?我尝试了以下代码,但是是否可以实现相同的目的?

Is there any way I can do have a Javascript class that extends an object that was created through the revealing module pattern? I tried the following code, but is there away to achieve the same thing?

sv.MergeQuestionViewModel = function () {
    this = sv.QuestionDetailViewModal();
    this.init($("#mergeQuestionModel"));
};  

sv.QuestionDetailViewModal = function () {
    var $el,
        self = this,
        _question = ko.observable(),
        _status = new sv.Status();

    var _init = function (el) {
        $el = el;
        $el.modal({
            show: false,
            backdrop: "static"
        });
    };

    var _show = function () {
        $el.modal('show');
    };

    var _render = function (item) {
        _question(new sv.QuestionViewModel(item));
        _show();
    };

    var _reset = function () {
        _question(null);
        _status.clear();
    };

    var _close = function () {
        $el.modal('hide');
        _reset();
    };

    return {
        init: _init,
        show: _show,
        render: _render,
        reset: _reset,
        close: _close
    };
};

推荐答案

您可以使用jQuery.extend来实现此行为.

You could use jQuery.extend to achive this behaviour.

sv.MergeQuestionViewModel = function () {
    $.extend(this, sv.QuestionDetailViewModal);

    this.init($("#mergeQuestionModel"));
};

sv.QuestionDetailViewModal = (function () {
 var el,

 _init = function($el) {
    el = $el;
    console.log('init', el);
 },

 _render = function() {
    console.log('render', el);
 };

 return {
   init : _init,
   render : _render
 };
}());

var view = new sv.MergeQuestionViewModel();
view.render();

http://jsfiddle.net/GEGNM/

这篇关于Javascript:混合构造函数模式和显示模块模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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