使用具有对象文字表示法和继承的命名空间 [英] Using namespacing with object literal notation and inheritance

查看:53
本文介绍了使用具有对象文字表示法和继承的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有对象文字符号的名称间隔. 我想使用原型对象继承,以便SceneSnippet的子类".

I am using name-spacing with object literal notation. I would like to use prototype object inheritance so that Scene is "subclass" of Snippet.

我无法正确启动Scene对象.

console.log(scen1 instanceof xxx.prototypes.Scene); // false

基本上Scene对象应该具有Scene中的所有属性和方法,以及Snippet中的所有方法,结果应为:

Basically Scene object should have all properties and methods from Scene + from Snippet and the result should be:

console.log(scen1 instanceof xxx.prototypes.Scene); // true

你知道我在做什么错吗?

Any idea what I am doing wrong here?

;
(function(xxx, window) {
    xxx.prototypes = {
        Snippet: function(snippetId) {
            var parent = this;
            this.id = snippetId;
            this.data;
            this.isVisible = 'test';
            this.isFocused = false;
            this.focus = function() {};
            this.render = function() {};
        },
        Scene: function(scenaId) {
            xxx.prototypes.Snippet.call(this);
            xxx.prototypes.Scene.prototype = Object.create(xxx.prototypes.Snippet.prototype);
            xxx.prototypes.Scene.prototype.constructor = xxx.prototypes.Snippet;
            xxx.prototypes.Scene.prototype.isScene = true;
            xxx.prototypes.Scene.prototype.isActive;
        }
    };
}(window.xxx= window.xxx|| {}, window));

//-----------------------------------------------------
function start() {
    var snip1 = new xxx.prototypes.Snippet('snip1');
    snip1.data = 'snippet-data-1';
    snip1.focus();

    var scen1 = new xxx.prototypes.Scene('scen1');
    scen1.data = 'scene-data-1';
    scen1.focus();      

    console.log(snip1 instanceof xxx.prototypes.Snippet); // TRUE
    console.log(scen1 instanceof xxx.prototypes.Scene); // FALSE PROBLEM HERE
}


答案

(function(xxx, window) {
    xxx.prototypes = {
        Snippet: function(snippetId) {
            var parent = this;
            this.id = snippetId;
            this.data;
            this.isVisible = 'test';
            this.isFocused = false;
            this.focus = function() {};
            this.render = function() {};
        },
        Scene: function(scenaId) {
            xxx.prototypes.Snippet.call(this, scenaId);
            this.isScene = true;
            xxx.prototypes.Scene.prototype.isActive;
        }
    };
}(window.xxx= window.xxx|| {}, window));



//-----------------------------------------------------
function start() {
    var snip1 = new xxx.prototypes.Snippet('snip1');
    snip1.data = 'snippet-data-1';
    snip1.focus();


    var scen1 = new xxx.prototypes.Scene('scen1');
    scen1.data = 'scene-data-1';
    scen1.focus();


    console.log(snip1 instanceof xxx.prototypes.Snippet); // TRUE
    console.log(scen1 instanceof xxx.prototypes.Scene); // TRUE
    console.log(scen1);
}
start();

推荐答案

您在此处更改了场景的原型:

you changed prototype of Scene here:

xxx.prototypes.Scene.prototype = Object.create(xxx.prototypes.Snippet.prototype);

查看有关Object.create的这篇文章

check this article about Object.create

https://developer.mozilla. org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

编辑->

   Scene: function(scenaId) {
            xxx.prototypes.Snippet.call(this, scenaId);
            this.isScene = true;
            xxx.prototypes.Scene.prototype.isActive;
   }

这篇关于使用具有对象文字表示法和继承的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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