小编peh*_*has的帖子

Javascript:如何正确扩展类

通过互联网搜索我总是碰到这种Javascript类扩展的方法

function extend(Child, Parent) {
    var F = function() { }
    F.prototype = Parent.prototype
    Child.prototype = new F()
    Child.prototype.constructor = Child
    Child.superclass = Parent.prototype
}
Run Code Online (Sandbox Code Playgroud)

但是这与那个有什么不同呢?

  function extend(Child, Parent) {
    var p = new Parent()
    Child.prototype = p
    Child.prototype.constructor = Child
    Child.superclass = p
  }
Run Code Online (Sandbox Code Playgroud)

最后一个也很完美.那我为什么要用这个额外的var F = function() { }动作呢?

javascript prototype

8
推荐指数
1
解决办法
623
查看次数

标签 统计

javascript ×1

prototype ×1