Javascript 如何将函数从主对象动态继承到其他对象?

Javascript 如何将函数从主对象动态继承到其他对象?,javascript,object,inheritance,Javascript,Object,Inheritance,在这种情况下,我想创建动态的新对象(某些动物),例如对象“游戏”下的青蛙。 在Game.frog里面我想从Game继承函数init到frog 因此,frog也将具有函数init 所有其他动物将克隆函数init Game.frog.init(), Game.lion.init() ...... Game.n...int() 动物将如下所示 非常感谢你的帮助 Game.frog = { init: function(){ this.property1 = somethin

在这种情况下,我想创建动态的新对象(某些动物),例如对象“
游戏”
下的
青蛙

Game.frog里面
我想从
Game
继承函数init到
frog
因此,frog也将具有函数
init
所有其他动物将克隆函数
init

 Game.frog.init(), Game.lion.init() ...... Game.n...int()
动物将如下所示

非常感谢你的帮助

Game.frog = {
    init: function(){
        this.property1 = something;
        this.property2 = something;
        this.property3 = something;
        this.property1000 = something;
    } 
};
我的代码:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
var Game = {
    init: function(){
        this.property1 = 1;
        this.property2 = 2;
        this.property3 = 3;
        this.property1000 = 1000;
    },

     cons: function(gameAnimals){

        for(var i = 0; i < gameAnimals.length; i++){
             this.gameAnimals[i] = {};
             this.gameAnimals[i].init() = this.init();
            //or         
            //gameAnimals[i].prototype = new Game();        // someAnimal inheritance from Game 
            //gameAnimals[i].prototype.constructor=gameAnimals[i];      // frog constructor
        }
     }    
};

var gameAnimals = ['frog', 'lion', 'cat'];
Game.cons(gameAnimals);
alert(Game.frog[0]+' '+Game.frog[1]+' '+Game.frog[2]+' '+Game.frog[2]);//display  1 2 3 1000
                                                                        //frog.property2 = 2;
                                                                        //frog.property3 = 3;
                                                                        //frog.property1000 = 1000;
}//]]>  

</script>


</head>
<body>


</body>


</html>

-JSFIDLE演示
//  

为什么不创建一个动物功能,然后为您的每一个小动物实例化:

function Animal() {
    self = this;
    self.init = function() {
        self.property1 = 1;
        self.property2 = 2;
        self.property3 = 3;
        self.property1000 = 1000;
    }
}

var frog = new Animal();
frog.init();

也可以读一读:了解JS继承模式。

你应该重新考虑你试图访问“动物”的方式。。。 试试这个:

window.onload = function () {

    function Animal(name) {
      this.name = name;
      this.property1 = 1;
      this.property2 = 2;
      this.property3 = 3;
      this.property1000 = 1000;
    }

    function Game() {
      this.animals = {};

      this.addAnimal = function (animalName) {
        this.animals[animalName] = new Animal(animalName);

      };
    }


    var game = new Game();

    game.addAnimal('frog');
    game.addAnimal('lion');
    game.addAnimal('cat');

    alert(game.animals.frog.name +"; " + game.animals.frog.property1 +"; " + game.animals.frog.property2);
  };

我想你应该写:this.gameanies[i].init=this.init;因此没有()调用该函数