John Resig 的 Javascript 继承片段是否已弃用?

2024-04-26

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

例如,有一个班级Animal and Dog,其中 Animal 类定义了一个方法makeSound()它建立了如何输出声音,然后 Dog 在它自己的中重写makeSound()方法发出“汪”声,同时也调用 Animal 的makeSound()输出那个低音。

我看了约翰·雷西格的模型here http://ejohn.org/blog/simple-javascript-inheritance/,但它使用本机arguments.callee该属性在 ECMA 脚本 5 中显然已贬值。这是否意味着我不应该使用 John Resig 的代码?

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


这是否意味着我不应该使用 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

John Resig 的 Javascript 继承片段是否已弃用? 的相关文章

  • 错误:集合方法聚合是同步的

    我正在尝试以下代码 const Conn mongoose createConnection mongodb 127 0 0 1 27017 db const addresses Conn collection users aggregat
  • Javascript .includes 函数无法与对象数组一起正常工作[重复]

    这个问题在这里已经有答案了 我有一个正在使用的对象数组 includes 功能 我正在使用数组中的对象搜索该数组 对象是相同的 但似乎没有匹配项 我已将问题复制到这把小提琴 https jsfiddle net 6dua0u0n 代码也在下
  • 如何使用数据对象中的值指定 d3.js 选择器?

    我在 Web 应用程序中使用 d3 js 描述我想要做的事情的最简单方法是查看下面链接的 Fiddle 但基本设置是我有一个包含数据对象的数组 my data id B text I want this text in B id C tex
  • yup - 逗号后允许两位数字,小数的最小值和最大值

    const validationSchema yup object amount yup number positive min 5 minimum 5 max 10 maximum 10 如何添加对逗号后两位数字的小数的验证 像这样解决
  • nodeJS require.paths 解决问题

    我试图相对且神秘地需要一个文件 以下情况正在发生 这很有效 这表明 Users marcos Desktop Taper lib utils js myPath Users marcos Desktop Taper lib utils re
  • 多重继承中使用delete操作符时谁调用类的析构函数

    这个问题听起来可能太愚蠢了 但是 我在其他地方找不到具体的答案 对后期绑定如何工作以及继承中使用的 virtual 关键字知之甚少 如代码示例中所示 在继承的情况下 当使用指向在堆上创建的派生类对象的基类指针和删除运算符来释放内存时 将仅按
  • JavaScript 和数据库连接

    javascript可以直接访问数据库吗 我觉得我的问题是反问 因为这是一个安全问题 但这有可能吗 有可能的 有了新的html5功能 js可以通过WebSql连接 一个活生生的例子 http html5demos com database
  • 如何防止脚本注入攻击

    Intro 这个话题一直是 StackOverflow 以及许多其他技术论坛上许多问题和答案的祸根 然而 其中大多数都是特定于具体条件的 甚至更糟 通过脚本注入预防中的 整体 安全性dev tools console or dev tool
  • 使用 Javascript 从 URL 字符串获取端口 [重复]

    这个问题在这里已经有答案了 我想要一个 javascript 函数 它将获取一个 url 作为参数 并返回该 URL 的端口 如下所示 如果有一个http or https 端口 80 443 它不会显示在 url 结构中 但我还是希望它们
  • 如何动态构造方法?

    我设计了一个类 它非常标准 具有一些方法属性 class foo def f1 self print f1 def f2 self print f2 def fn self print fn 现在我想创建一个包含一组 foo 实例的类 cl
  • 单击按钮时执行 python 脚本

    我有一个带有一个按钮的 HTML 页面 当我们单击该按钮时 我需要执行一个 python 脚本 并返回到包含结果的同一 HTML 页面 所以我需要对返回值进行一些验证并执行一些操作 这是我的代码 HTML
  • 客户端上传并读取文件,角度为2

    我需要用户的日志文件 以便我可以读取和分析这些文件 例如某种放置区域 用户放置一个文件 然后我可以用javascript读取它 我使用 Angular2 rc5 我有 node js 在后台运行 但我不需要那里的数据 我只需要在客户端 是否
  • UserDict 类的优点?

    使用有什么好处UserDict class 我的意思是 我真正得到的不是 class MyClass object def init self self a 0 self b 0 m MyClass m a 5 m b 7 我将写下以下内容
  • canvas:如何在一个变换语句中完成平移、倾斜、旋转...?

    最近几天我在学习 变换 现在我知道如何通过变换的矩阵进行平移 旋转 倾斜 缩放 但如果我想在一个转换语句中执行上述所有操作 我该怎么办 ctx transform a b c d e f 当我们想要通过变换旋转某些东西时 我们必须为每个参数
  • 如何在 jQuery 中检查 null 对象

    我正在使用 jQuery 我想检查页面中是否存在某个元素 我写了以下代码 但它不起作用 if btext i null alert btext i text btext i text Branch i 如何检查元素是否存在 检查jQuery
  • 我们可以使用 axios 的 onDownloadProgress 来加载 API 吗?

    我需要使用 axios 创建一个用于在 React 项目中加载 API 的进度条 我为此发现了 onDownloadProgress 函数 但我不知道我们是否可以使用它来获取诸如加载百分比之类的信息 或者它是否仅用于文件下载 所以我不确定我
  • jQuery Deferred - 向 Deferred 合约添加回调

    我正在尝试在现有 Deferred 的状态设置为成功之前向其合约添加另一个异步调用 不要尝试用英语解释这一点 请参阅以下伪代码 when ajax url someUrl data data async true success funct
  • Socket.io 不断重复连接并忽略其他事件

    我正在尝试制作一个网络应用程序 用户可以在其中互相玩环形国际象棋 这是我的 app js 在服务器上 var express require express var app express var http require http Ser
  • 在 JavaScript 中添加事件的最佳方式是什么?

    我发现在 JavaScript 中设置事件有两种主要方法 直接在标签内添加一个事件 如下所示 a href do foo a 通过 JavaScript 设置它们 如下所示 a href do bar a 并在 a 中添加一个事件
  • 类型错误: jasmine.getEnv().currentSpec 为 null

    当我尝试运行我的茉莉花规格时 我得到 TypeError jasmine getEnv currentSpec is null in http localhost 8888 JASMINE ROOT jasmine js line 498

随机推荐