0

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

I am not able to initiate Scene object properly.

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:

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();
2
  • 1
    may be this is not what you want as resalt but here is the link jsfiddle.net/z7uGA/3 Commented Oct 30, 2013 at 10:53
  • thanks max it solved the problem, please add it as your answer. I would be glad to accept it and up vote it. Thanks for your help on this Commented Oct 30, 2013 at 11:35

1 Answer 1

1

you changed prototype of Scene here:

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

check this article about Object.create

https://developer.mozilla.org/en-US/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;
   }
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know purpose what you want to achieve but here is the link jsfiddle.net/z7uGA
Please try this jsfiddle jsfiddle.net/z7uGA/1 as you can see I cannot assign id using Scene

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.