首页 > Web开发 > 详细

2014-02-27_javascript_PrototypalInheritance

时间:2014-02-28 17:56:48      阅读:464      评论:0      收藏:0      [点我收藏+]

//Object.create() 方法

if (typeof Object.create !== "function")
    Object.create = function(o) {
      function F() {}
      F.prototype = o;
      return new F();
    };

var Model = {
  inherited: function(){},
  created: function(){},
  prototype: {
    init: function(){}
  },
  create: function(){
    var object = Object.create(this);
    object.parent = this;
    object.prototype = object.fn = Object.create(this.prototype);
    object.created();
    this.inherited(object);
    return object;
  },
  init: function(){
    var instance = Object.create(this.prototype);
    instance.parent = this;
    instance.init.apply(instance, arguments);
    return instance;
  }
};

 

var Asset = Model.create();
var User  = Model.create();
var user = User.init();

 

注意:javascript中执行typeof Object=="funcion"

 

2014-02-27_javascript_PrototypalInheritance,布布扣,bubuko.com

2014-02-27_javascript_PrototypalInheritance

原文:http://dragonwarrior.blog.51cto.com/2729771/1364690

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