首页 > 编程语言 > 详细

JavaScript基于原型的继承

时间:2015-05-31 20:06:23      阅读:284      评论:0      收藏:0      [点我收藏+]

 

在一个纯粹的原型模式中,我们会摒弃类,转而专注于对象,基于原型的继承相比基于类的继承的概念上更为简单

if( typeof Object.beget  !== ‘function‘)
{
     Object.beget = function(o) {
               var F = function() {};
               F.prototype = o;
               return new F();
      }
}
var myMammal = {
      
      name : ‘Herb the Mammal‘,
      get_name : function() {
                return this.name;
         },
        says : function() {
                 return this.saying || ‘ ‘;
           }
};
var myCat = Object.beget(myMammal);
myCat.name = ‘Henrietta‘;
myCat.saying = ‘meow‘;
myCat.get_name = function() {
  return this.says + ‘  ‘+ this.name + ‘ ‘+this.says;
};

  

 

JavaScript基于原型的继承

原文:http://www.cnblogs.com/zenus/p/4542478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!