面试题答案
一键面试- 寄生组合继承模式实现继承体系
- 定义顶层父类
GameObject
:
- 定义顶层父类
function GameObject() {
this.commonProperty = 'I am a game object';
this.commonMethod = function () {
console.log('This is a common method of GameObject');
};
}
- 定义
Character
类继承自GameObject
:
function Character() {
GameObject.call(this);
this.characterProperty = 'I am a character';
}
Character.prototype = Object.create(GameObject.prototype);
Character.prototype.constructor = Character;
- 定义
Player
类继承自Character
:
function Player() {
Character.call(this);
this.playerProperty = 'I am a player';
}
Player.prototype = Object.create(Character.prototype);
Player.prototype.constructor = Player;
- 定义
Enemy
类继承自Character
:
function Enemy() {
Character.call(this);
this.enemyProperty = 'I am an enemy';
}
Enemy.prototype = Object.create(Character.prototype);
Enemy.prototype.constructor = Enemy;
- 处理属性和方法的继承与重写
- 继承:通过寄生组合继承模式,子类会继承父类原型上的方法和属性。例如,
Player
和Enemy
都继承了Character
的属性和方法,而Character
又继承了GameObject
的属性和方法。 - 重写:如果子类需要重写父类的方法,直接在子类的原型上定义同名方法即可。例如,如果
Player
需要重写commonMethod
:
- 继承:通过寄生组合继承模式,子类会继承父类原型上的方法和属性。例如,
Player.prototype.commonMethod = function () {
console.log('This is a overridden common method in Player');
};
- 处理命名冲突问题
- 命名规范:使用前缀或命名空间来避免命名冲突。例如,
GameObject
相关的属性和方法可以使用gameObject_
前缀,Character
相关的可以使用character_
前缀等。 - 使用闭包和模块化:将相关的类和功能封装在闭包或模块中,限制变量和函数的作用域。例如,使用ES6模块:
- 命名规范:使用前缀或命名空间来避免命名冲突。例如,
// gameObject.js
export function GameObject() {
this.gameObject_commonProperty = 'I am a game object';
this.gameObject_commonMethod = function () {
console.log('This is a common method of GameObject');
};
}
// character.js
import {GameObject} from './gameObject.js';
export function Character() {
GameObject.call(this);
this.character_property = 'I am a character';
}
Character.prototype = Object.create(GameObject.prototype);
Character.prototype.constructor = Character;
// player.js
import {Character} from './character.js';
export function Player() {
Character.call(this);
this.player_property = 'I am a player';
}
Player.prototype = Object.create(Character.prototype);
Player.prototype.constructor = Player;
这样不同模块中的变量和函数不会相互干扰,减少命名冲突的可能性。