オブジェクトリテラル表記で名前空間を使用しています。Scene
の「サブクラス」になるように、プロトタイプ オブジェクトの継承を使用したいと思いますSnippet
。
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();