简体   繁体   English

使用具有对象文字表示法和继承的命名空间

[英]Using namespacing with object literal notation and inheritance

I am using name-spacing with object literal notation. 我正在使用带有对象文字表示法的名称间隔。 I would like to use prototype object inheritance so that Scene is "subclass" of Snippet . 我想使用原型对象继承,以便SceneSnippet “子类”。

I am not able to initiate Scene object properly. 我无法正确启动Scene对象。

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

Basically Scene object should have all properties and methods from Scene + from Snippet and the result should be: 基本上, Scene对象应具有Snippet Scene +中的所有属性和方法,结果应为:

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
}

Answer 回答

(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);

check this article about Object.create 查看有关Object.create的本文

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

edited -- > 编辑->

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM