我正在学习js继承和原型设计,我的命名可能完全不对,我很抱歉。
我正在尝试创建一个超级对象和原型 2 子对象作为属性,然后在其中一个子对象中调用在另一个子对象中找到的函数。 由于某种原因,它不起作用。
更新
我的目标是:我正在尝试制作一个小游戏 - 为了乐趣和练习。我的计划是有 1 个名为 (object) 的基本对象,该对象具有定位和其他属性(每个其他对象都将具有)另一个称为(控件)的对象用于控件。只有可以移动的对象才会具有该对象。
玩家也是对象,他们将同时拥有"对象"和"控件"。 作为他们的原型。
希望能把事情弄清楚一点。
法典:
// sub Object1
function object(){
this.speed = 1;
this.walkDistant = 5;
}
// sub Object2
function controls(){
this.moveLeft = function(){
console.log(this.speed , this.walkDistant);
return this.speed * this.walkDistant;
}
}
// super Object
function player(){
// DoesNothing
}
player.prototype.object = new object();
player.prototype.controls = new controls();
var firstPlayer = new player();
console.log(firstPlayer.controls.moveLeft());
或者如果你更喜欢小提琴:http://jsfiddle.net/rMaKa/1/
控制播放器,所以你可以将控件与播放器混合。你的对象构造函数是一个错误选择的名称,因为构造函数应该以大写字母开头,使其成为 Object,你会覆盖窗口。对象(坏主意)。出于这个原因,我已将其重命名为Base。播放器是一个基本对象,可以控制,以便从基本继承并混合控件。
有关构造函数、混合函数、实例成员和原型的更多信息,请查看此链接。
function Base() {
this.speed = 1;
this.walkDistant = 5;
}
// sub Object2
function Controls() {
}
Controls.prototype.moveLeft = function() {
console.log(this.speed, this.walkDistant);
return this.speed * this.walkDistant;
}
// super Object
function Player() {
//make player have Base instance members
Base.call(this);
//make player heve Controls instance members
Controls.call(this);
}
//player is a base object
Player.prototype = Object.create(Base.prototype);
//repair constrictor
Player.prototype.constructor = Player;
//Player can be controlled, copy controls prototype on player (mixin)
// this would be better suited in a helper function, see link posted in answer
var stuff;
for (stuff in Controls.prototype) {
if (Controls.prototype.hasOwnProperty(stuff)) {
Player.prototype[stuff] = Controls.prototype[stuff];
}
}
var firstPlayer = new Player();
console.log(firstPlayer.moveLeft());
如果你想让玩家有控件,你可以尝试这样的事情:
function Controls(what) {
//what do we need to control
this.controlWhat=what;
}
Controls.prototype.moveLeft = function() {
console.log(this.controlWhat.speed, this.controlWhat.walkDistant);
return this.controlWhat.speed * this.controlWhat.walkDistant;
};
function Player() {
this.speed = 1;
this.walkDistant = 5;
this.controls=new Controls(this);
}
var firstPlayer = new Player();
console.log(firstPlayer.controls.moveLeft());
问题是您正在尝试访问一个属性,该属性可以从subObj2
subObj1
,但是继承两者的superObj
。
为此,您应该使您的subObj1
继承subObj2
。
// sub Object1
function name(){
this.name = function(){
var myName = 'FirstName';
console.log(myName, this.last.lastName);
}
this.callName = function(){
this.name();
};
}
// sub Object2
function lastName(){
this.lastName ='someLastName';
}
// super Object
function fullName(){
// DoesNothing
}
name.prototype.last = new lastName();
fullName.prototype.name = new name();
var myName = new fullName();
myName.name.callName();
你可以看到这个小提琴
您可以使用 Mixins 使用已在其他对象中实现的功能来扩展对象的功能。你也可以让子对象知道超级对象,如下所示。
function subClassA(containerClass) {
this.containerClass = containerClass;
this.methodA = function() {
console.log(this.containerClass.b.methodB());
}
}
function subClassB(containerClass) {
this.containerClass = containerClass;
this.methodB = function() {
return 12345;
}
}
function containerClass() {
this.a = new subClassA(this);
this.b = new subClassB(this);
}
var cc = new containerClass();
cc.a.methodA();
Mixin 方法如下所示:
// extend function to add mixin support
function extend(destination, source) {
for (var k in source)
if (source.hasOwnProperty(k))
destination[k] = source[k];
return destination;
}
function subClassA() { }
subClassA.prototype.methodA = function() {
console.log(this.methodB());
};
function subClassB() { }
subClassB.prototype.methodB = function() {
return 12345;
};
function superClass() {
// ----------------
}
// add the subClassA and subClassB functionality
extend(superClass.prototype, subClassA.prototype);
extend(superClass.prototype, subClassB.prototype);
var sc = new superClass();
sc.methodA();