使用带有对象文字符号和继承的命名空间

时间:2013-10-30 08:20:47

标签: javascript javascript-objects

我使用名称间距和对象文字表示法。 我想使用原型对象继承,以便SceneSnippet的“子类”。

我无法正确发起Scene个对象。

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

基本上Scene对象应该包含Scene +来自Snippet的所有属性和方法,结果应为:

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

知道我在这里做错了吗?

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

1 个答案:

答案 0 :(得分:1)

你在这里改变了场景原型:

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

查看有关Object.create

的这篇文章

https://developer.mozilla.org/en-US/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;
   }