Javascript原型最佳实践

时间:2016-04-28 08:08:44

标签: javascript prototype-programming

我正在尝试从SubClass到MainClass执行一个方法。 它适用于此代码,但有最好的方法(不要在参数中使用“_parent”)吗? 非常感谢你的帮助!

==> https://jsfiddle.net/jdrwwmt5/

var APP;
    (function () {
        "use strict";
        APP = function (id) {
            this.name = "MainClass"
            this.id = id;
            this.init();
        };
        APP.prototype = {
            init: function () {
                this.subClass = new APP.Sub(this);
            },
            onSubClassReady: function (_origin) {
                console.log(this);
                console.log(this.name);
                console.log(_origin);
                console.log(_origin.name);
            },
        };
    }());
    (function () {
        "use strict";
        var Sub = APP.Sub = function (_parent) {
            this.name = "SubClass"
            this.parent = _parent;
            this.isReady = false;
            this.initialize();
        };
        Sub.prototype = {
            initialize: function () {
               //...
               this.ready();
            },
            ready:function (){
                  this.isReady = true;
                  this.parent.onSubClassReady(this);
            },
        }
    }());
    var app1 = new APP('1');

var APP; (function () { "use strict"; APP = function (id) { this.name = "MainClass" this.id = id; this.init(); }; APP.prototype = { init: function () { this.subClass = new APP.Sub(this); }, onSubClassReady: function (_origin) { console.log(this); console.log(this.name); console.log(_origin); console.log(_origin.name); }, }; }()); (function () { "use strict"; var Sub = APP.Sub = function (_parent) { this.name = "SubClass" this.parent = _parent; this.isReady = false; this.initialize(); }; Sub.prototype = { initialize: function () { //... this.ready(); }, ready:function (){ this.isReady = true; this.parent.onSubClassReady(this); }, } }()); var app1 = new APP('1');

0 个答案:

没有答案