JavaScript:extend和prototype用于什么?

JavaScript:extend和prototype用于什么?,javascript,prototype,object-model,Javascript,Prototype,Object Model,我对JavaScript比较陌生,并一直在使用第三方库中查看.extend和.prototype。我认为这与原型javascript库有关,但我开始认为情况并非如此。这些用于什么?Javascript的继承是基于原型的,因此您可以扩展对象的原型,例如日期、数学,甚至您自己的自定义对象 Date.prototype.lol = function() { alert('hi'); }; ( new Date ).lol() // alert message 在上面的代码片段中,我为所有日期对象

我对JavaScript比较陌生,并一直在使用第三方库中查看.extend和.prototype。我认为这与原型javascript库有关,但我开始认为情况并非如此。这些用于什么?

Javascript的继承是基于原型的,因此您可以扩展对象的原型,例如日期、数学,甚至您自己的自定义对象

Date.prototype.lol = function() {
 alert('hi');
};

( new Date ).lol() // alert message
在上面的代码片段中,我为所有日期对象(现有对象和所有新对象)定义了一个方法

extend
通常是一个高级函数,它复制要从基类扩展的新子类的原型

因此,您可以执行以下操作:

extend( Fighter, Human )
Fighter
构造函数/对象将继承
Human
的原型,因此如果您在
Human
上定义
live
die
等方法,那么
Fighter
也将继承这些方法

最新澄清:

“高级函数”的意思是.extend不是内置的,但通常由jQuery或Prototype等库提供。

.extend()
由许多第三方库添加,以便于从其他对象创建对象。有关一些示例,请参见或


.prototype
指的是对象的“模板”(如果你想这样称呼它),因此通过向对象的原型中添加方法(你在库中可以看到很多添加到字符串、日期、数学甚至函数中的方法),这些方法将添加到该对象的每个新实例中

extend方法(例如在或中)将所有属性从源对象复制到目标对象

关于
prototype
属性,它是函数对象的成员,是语言核心的一部分

任何函数都可以用作,以创建新的对象实例。所有函数都具有此
prototype
属性

在函数对象上使用带的
new
运算符时,将创建一个新对象,并且它将从其构造函数
prototype
继承

例如:

function Foo () {
}
Foo.prototype.bar = true;

var foo = new Foo();

foo.bar; // true
foo instanceof Foo; // true
Foo.prototype.isPrototypeOf(foo); // true

Javascript继承似乎到处都是公开辩论。这可以称为“Javascript语言的奇案”

其思想是有一个基类,然后扩展基类以获得类似于继承的特性(不是完全的,但仍然如此)

整个想法是得到原型的真正含义。直到我看到John Resig的代码(接近jQuery.extend的功能)编写了一个代码块,他声称base2和原型库是灵感的来源,我才明白这一点

这是代码

    /* Simple JavaScript Inheritance
     * By John Resig http://ejohn.org/
     * MIT Licensed.
     */  
     // Inspired by base2 and Prototype
    (function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};

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

    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;

    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[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, prop[name]) :
        prop[name];
    }

    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }

    // Populate our constructed prototype object
    Class.prototype = prototype;

    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;

    return Class;
  };
})();
有三个部分正在做这项工作。首先,循环遍历属性并将它们添加到实例中。之后,您将为以后添加到对象中的对象创建构造函数。现在,关键行是:

// Populate our constructed prototype object
Class.prototype = prototype;

// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
首先将
类.prototype
指向所需的原型。现在,整个对象已更改,这意味着您需要强制布局回到其自己的布局

以及使用示例:

var Car = Class.Extend({
  setColor: function(clr){
    color = clr;
  }
});

var volvo = Car.Extend({
   getColor: function () {
      return color;
   }
});

请在的帖子中阅读更多相关信息。

第三方库中的一些
扩展功能比其他功能更复杂。例如,包含一个最简单的检查,它没有jQuery所做的一些检查:

function extend(target, source) {
    if (source) {
        for(var prop in source) {
            if(source.hasOwnProperty(prop)) {
                target[prop] = source[prop];
            }
        }
    }
    return target;
}
  • .extends()
    创建一个属于另一个类的子类。
    幕后
    Child.prototype.\uuu proto\uuu
    将其值设置为
    Parent.prototype

    以便继承方法。
  • .prototype
    将功能从一个继承到另一个。
  • 。\uuuu proto\uuuu
    是原型的一个getter/setter

    • 对我来说,这似乎是最清楚、最简单的例子,它只是附加属性或替换现有属性

      function replaceProperties(copyTo, copyFrom)  {
        for (var property in copyFrom) 
          copyTo[property] =  copyFrom[property]
        return copyTo
      }
      

      “高级函数”的意思是
      .extend
      不是内置的,但通常由jQuery或Prototype之类的库提供。我想补充一点,不建议在中扩展本机对象的原型JS@meder-您应该在答案中添加visum注释。:)在现代Javascript编程中,通常将全局对象和本地对象视为公共浴室的元素;你不能避免进去,但你应该尽量减少与表面的接触。这是因为
      更改本机对象可能会打破其他开发人员对这些对象的假设,
      导致javascript错误,这通常需要花费数小时来跟踪。这个答案的前导句似乎曲解了这个有价值的javascript实践。这应该是.extend()而不是.extends()?如果你想了解更多关于这个原型的东西,我建议你在khan academy上使用