在Javascript中继承对象的值

Inherit the value from object in Javascript

本文关键字:对象 继承 Javascript      更新时间:2023-09-26

我创建了一个animal类,其中包含了animal对象及其特征。

animal = function(){
    this.animalType = {
       tiger:{
         Character:[{
              Height: ,
              Color:'',
              Name:''
         }]
       },
       Lion:{
          Character:[{
             Height: ,
            Color:'',
            Name:''
       }]
       }
    };
};
  animal.prototype.getType = function(type){
  };

我想写一个方法来获得动物的特征

这是你的动物类应该是什么样子的一个例子。演示

var animal = function(o){
    this.type = o.type || 'unknown';
    this.height = o.height || -1;
    this.color = o.color || 'unknown';
    this.name = o.name || 'unknown';
}
animal.prototype.getType = function(){
    return this.type;
}
var tiger = new animal({
    type: 'tiger',
    height: 120,
    color: 'red',
    name: 'billy'
});
alert(tiger.getType());