简体   繁体   English

不懂JavaScript原型

[英]Don't understand JavaScript prototype

I'm reading JavaScript Good Parts. 我正在阅读JavaScript Good Parts。 There is an example: 有一个例子:

if (typeof Object.create !== 'function') {
    Object.create = function(o) {
        var F = function(){};
        F.prototype = o;
        return new F();
    }
}

Can somebody explain to me what this means? 有人可以向我解释这意味着什么吗? To begin with, Object already has a method with this name because when I run this: 首先,Object已经有一个具有此名称的方法,因为当我运行它时:

console.log(typeof Object.create);

The output is 'function'? 输出是'功能'?

To answer your "To begin with...", you might still run into Javascript environments where 要回答你的“开头......”,你可能还会遇到Javascript环境

console.log(typeof Object.create);

will report 将报告

undefined

or some such (assuming you even have a console object with a log function available.) The reason for that outer if (typeof Object.create !== 'function') wrapper is to only define this in the case that your JS environment doesn't already do so. 或者某些这样的(假设你甚至有一个带有日志功能的控制台对象。)外部if (typeof Object.create !== 'function')包装器的原因是只在你的JS环境没有的情况下定义它我们已经这样做了。 There are many older browsers and other environments where your code might one day run that could conceivably not have Object.create defined. 有许多旧的浏览器和其他环境,您的代码可能有一天会运行,可能无法定义Object.create

Now as to how this actual function works, its based on how JS handles objects. 现在关于这个实际函数是如何工作的,它基于JS处理对象的方式。 Objects are simply collections of named properties, or more properly associations between String names and property values. 对象只是命名属性的集合,或者是String名称和属性值之间更恰当的关联。 But many also have a special property, which is their prototype . 但是许多人也有一个特殊的属性,这是他们的prototype This is simply a pointer to another object. 这只是指向另一个对象的指针。 That object can also have its own prototype object, and so on. 该对象也可以拥有自己的prototype对象,依此类推。 Eventually, though, the prototype chain dies out when the prototype of one of these objects is null. 但最终,当其中一个对象的prototype为null时,原型链就会消失。 These prototype objects also are collections of named properties, and when the Javascript engine is searching your object for a named property, if it doesn't find it directly on your object, it checks to see if your object's prototype might contain it, and if not, if the prototype of that object might contain it, and so on, until the chain dies out. 这些原型对象也是命名属性的集合,当Javascript引擎在您的对象中搜索命名属性时,如果它没有直接在您的对象上找到它,它会检查您的对象的原型是否可能包含它,如果不,如果该对象的原型可能包含它,依此类推,直到链条消失。

The point of these prototype objects is that they can be shared. 这些原型对象的重点是它们可以共享。 Several objects, or even several million objects, can share a single prototype. 几个对象,甚至几百万个对象,可以共享一个原型。 This is substantially different from how class-based inheritance schemes work, but it can often be put to a similar purpose. 这与基于类的继承方案的工作方式大不相同,但通常可以用于类似的目的。 You can define a single copy of a function, and all objects created from a common constructor function will use that one copy. 您可以定义函数的单个副本,并且从公共构造函数创建的所有对象都将使用该副本。 Before such techniques as Object.create were created, this was the only real means of doing Object-oriented programming in Javascript. 在创建Object.create等技术之前,这是在Javascript中进行面向对象编程的唯一真正方法。

Crockford's code is using that older technique to simulate part of what the newer technique is supposed to do. Crockford的代码使用旧技术来模拟新技术应该做的部分内容。 Object.create defines some behaviors that can't really be achieved through the older mechanisms, but the most basic behaviors are simply to create a new object whose prototype is the specified object. Object.create定义了一些通过旧机制无法实现的行为,但最基本的行为只是创建一个原型是指定对象的新对象。 That's exactly what the older technique, using F.prototype = o; return new F(); 这正是旧技术,使用F.prototype = o; return new F(); F.prototype = o; return new F(); accomplished. 实现。 So this code is pretty much the standard way to shim the new behavior into older environments. 因此,此代码几乎是将新行为填充到旧环境中的标准方法。 For another example of this, see the MDN create page 有关此示例,请参阅MDN创建页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM