John Resig的Javascript继承代码段是否已被弃用?

CL2*_*L22 24 javascript oop inheritance prototypal-inheritance

我正在寻找一种简单的方法来创建两个类,一个继承自另一个类,子类重新定义父方法之一,并在新方法内调用父类.

例如,有一个类,Animal并且Dog,Animal类定义了一个makeSound()确定如何输出声音的方法,然后Dog在其自己的makeSound()方法中覆盖以产生"低音"声音,但同时也调用Animal makeSound()来输出那个低音.

我在这里查看了John Resig的模型,但它使用了arguments.calleeECMA脚本中明显折旧的本机属性.这是否意味着我不应该使用John Resig的代码?

使用Javascript的原型继承模型编写动物/狗代码的简洁方法是什么?

Ber*_*rgi 27

这是否意味着我不应该使用John Resig的代码?

正确,而不是在严格模式下使用ES5时.但是,它可以很容易地适应:

/* Simple JavaScript Inheritance for ES 5.1
 * based on http://ejohn.org/blog/simple-javascript-inheritance/
 *  (inspired by base2 and Prototype)
 * MIT Licensed.
 */
(function(global) {
  "use strict";
  var fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  function BaseClass(){}

  // Create a new Class that inherits from this class
  BaseClass.extend = function(props) {
    var _super = this.prototype;

    // Set up the prototype to inherit from the base class
    // (but without running the init constructor)
    var proto = Object.create(_super);

    // Copy the properties over onto the new prototype
    for (var name in props) {
      // Check if we're overwriting an existing function
      proto[name] = typeof props[name] === "function" && 
        typeof _super[name] == "function" && fnTest.test(props[name])
        ? (function(name, fn){
            return function() {
              var tmp = this._super;

              // Add a new ._super() method that is the same method
              // but on the super-class
              this._super = _super[name];

              // The method only need to be bound temporarily, so we
              // remove it when we're done executing
              var ret = fn.apply(this, arguments);        
              this._super = tmp;

              return ret;
            };
          })(name, props[name])
        : props[name];
    }

    // The new constructor
    var newClass = typeof proto.init === "function"
      ? proto.hasOwnProperty("init")
        ? proto.init // All construction is actually done in the init method
        : function SubClass(){ _super.init.apply(this, arguments); }
      : function EmptyClass(){};

    // Populate our constructed prototype object
    newClass.prototype = proto;

    // Enforce the constructor to be what we expect
    proto.constructor = newClass;

    // And make this class extendable
    newClass.extend = BaseClass.extend;

    return newClass;
  };

  // export
  global.Class = BaseClass;
})(this);
Run Code Online (Sandbox Code Playgroud)


nea*_*sic 7

Prototype chain with Object.create() + assign constructor

function Shape () {
    this.x = 0;
    this.y = 0;
}

Shape.prototype.move = function (x, y) {
    this.x += x;
    this.y += y;
};

function Rectangle () {
    Shape.apply(this, arguments); // super constructor w/ Rectangle configs if any
}

Rectangle.prototype = Object.create(Shape.prototype); // inherit Shape functionality
// works like Rectangle.prototype = new Shape() but WITHOUT invoking the constructor

Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

rect instanceof Rectangle && rect instanceof Shape // returns true
Run Code Online (Sandbox Code Playgroud)

from Object.create documentation

information about the new keyword

  • 这是2015年实现这一目标的方式 (2认同)