John Resig 的 Javascript 继承片段是否已弃用? [英] Is John Resig's Javascript inheritance snippet deprecated?

查看:25
本文介绍了John Resig 的 Javascript 继承片段是否已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a simple way of creating two classes, one inheriting from the other, and the child redefining one of the parent's methods, and inside the new method, calling the parent's.

例如,有一个类AnimalDog,其中Animal 类定义了一个方法makeSound(),该方法建立了如何输出一个声音,然后 Dog 在其自己的 makeSound() 方法中覆盖以发出woof"声音,同时还调用 Animal 的 makeSound() 以输出该woof.

For example, having a class Animal and Dog, where the Animal class defines a method makeSound() which establishes how to output a sound, which Dog then overrides in its own makeSound() method to make a "woof" sound, but while also calling Animal's makeSound() to output that woof.

我在此处查看了 John Resig 的模型,但它使用了本机 arguments.callee 在 ECMA 脚本 5 中明显贬值的属性.这是否意味着我不应该使用 John Resig 的代码?

I looked at John Resig's model here, but it uses the native arguments.callee property which is apparently depreciated in ECMA script 5. Does that mean I shouldn't use John Resig's code?

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

What would one neat, simple way of writing my animal/dog code using Javascript's prototype inheritance model?

推荐答案

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

Does that mean I shouldn't use John Resig's code?

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

Correct, not when you are using ES5 in strict mode. However, it can be easily adapted:

/* 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;}) ? /_super/ : /.*/;

  // 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);

这篇关于John Resig 的 Javascript 继承片段是否已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆