本文介绍了Crockford Prototypical Inheritance ......没有原型链?没有超级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试更多地了解Crockford的原型继承方法,从而基本上消除了构造函数,从而消除了原型链或利用超级概念的任何真实可能性。

Trying to understand a little more about Crockford's approach to Prototypical Inheritance whereby he essentially eliminates the constructor, thus eliminating any real possibility for a prototype chain or leveraging the idea of "super".

mynamespace.object.create = function( o ) {
    function F(){};
    F.prototype = o;
    return new F();
};

这是故意基于原型背后的理论吗?

Is that intentional based on the theory behind prototypical?

我遇到这个的原因是我想用一个init方法创建一个基类,它做了一些常见的jquery东西(这些方法用于自动创建一个jquery插件来自一个对象www.alexsexton.com/ ?p = 51)

The reason I ran into this is I wanted to create a base class with an init method that did some common jquery stuff (these methods are designed for automating creating a jquery plugin from an object a la www.alexsexton.com/?p=51)

作为一个例子,我不想在我的每个init方法中重复这一点
this.options = $ .extend ({},this.options,options);

As an example, I don't want to repeat this in every one of my init methods this.options = $.extend({},this.options,options);

所以我希望在基本init中使用它,覆盖扩展对象中的init,然后调用它。在override中使用prototype.init来处理那些重复的东西。

So I wanted to have that in a base init, override the init in my extended objects, and then call this.prototype.init within the override to take care of that repeated stuff.

object.create片段似乎不允许我以任何方式这样做。我在这里遗漏了什么吗?

The object.create piece doesn't seem to let me do that in any way. Am I missing something here?

推荐答案

该技术以原型链为中心,这种类型的继承也被称为差异继承

The technique is centered on the prototype chain, this type of inheritance is also known as differential inheritance.

例如:

var obj = {};
var obj1 = Object.create(obj);
var obj2 = Object.create(obj1);

obj2 的原型链看起来像这样:

The prototype chain of obj2 looks like this:


           --------        --------         ------------------
 obj2 ---> | obj1 | -----> | obj  | -----> | Object.prototype | -----> null
           --------        -------          ------------------

上面示例中连接对象的箭头是内部 [[Prototype]] 属性,以及形成原型链。

The arrow that connects the objects in the above example is the internal [[Prototype]] property, and that forms the prototype chain.

当您尝试访问某个属性时,例如在 obj2 中,它将是搜索原型链上的所有对象,直到找到,否则属性访问者只会产生 undefined

When you try to access a property, for example in obj2, it will be searched along all the objects on the prototype chain until is found, otherwise the property accessor will simply yield undefined

super 的概念并不存在,尽管有方法可以知道对象的 [[Prototype]]

The concept of super doesn't really exist, although there are ways to know the [[Prototype]] of an object.

ECMAScript第5版引入了 Object.getPrototypeOf 方法,例如:

The ECMAScript 5th Edition introduced the Object.getPrototypeOf method, for example:

Object.getPrototypeOf(obj2) === obj1; // true

但是这个方法是(以及标准 Object.create )。

However this method is not widely supported yet (along with the standard Object.create).

某些实现通过 __ proto __ [[Prototype]] 的访问权限> property,例如:

Some implementations provide access to an object's [[Prototype]] through the __proto__ property, e.g.:

obj2.__proto__ === obj1; // true

但请记住 __ proto __ 是非标准的。

isPrototypeOf 方法是ECMAScript 3的一部分,它允许您知道对象是否在另一个的原型链,例如:

The isPrototypeOf method, part of ECMAScript 3, allows you to know if an object is in the prototype chain of another, e.g.:

obj1.isPrototypeOf(obj2);             // true
obj.isPrototypeOf(obj2);              // true
Object.prototype.isPrototypeOf(obj2); // true

总之,在原型链中很快就会解析属性,如果你想避免 __ proto __ Object.getPrototypeOf (因为第一个是非标准的,前者不受广泛支持但是,我只是建议你在基础对象中加上 init 方法的前缀,这样就可以从更具体的实例访问它,例如:

In conclusion, a property is resolved as soon is found in the prototype chain, and if you want to avoid __proto__ and Object.getPrototypeOf (since the first is non-standard and the former is not widely supported yet), I would simply recommend you to prefix your init method in the base object, so it can be accessible from the more specific instances, e.g.:

var base = {
  baseInit: function () { /*...*/ }
};

var specific = Object.create(base);
specific.init = function () {
  //...
  this.baseInit();
  //...
};

specific.init();

这篇关于Crockford Prototypical Inheritance ......没有原型链?没有超级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:24