JavaScript中的原型和构造函数(plain English)? [英] Prototype and constructor in JavaScript (plain English)?

查看:175
本文介绍了JavaScript中的原型和构造函数(plain English)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


JavaScript是世界上最误解的语言-D.Crockford

我的问题:


  1. 简单的英语中的构造函数和原型?

  2. 使用原型?使用
    原型和构造函数后面的目的是什么?我的意思是他们提供更多的
    的灵活性。我问这个问题,因为我在过去六个月中一直在使用这种语言
    ,从来没有使用过原型和
    构造函数的情况。

我不是在寻找任何语法,以及如何去做事情的解释,因为我明白他们的一部分,只是想以更简单的方式知道这些事情。 *

I am not looking for any syntax and how to go about things explanations as I do understand some part of what they are, just wanted to know these things in a simpler way. An analogy (non-tech) or example would be great.*

详细信息为什么我问这个问题(忽略如果你想):

Details why I asked this question (ignore if you want to):

过去六个月我一直在使用JavaScript,当我发现JavaScript是基于原型的语言时,我感到非常震惊。

I have been using JavaScript for the past six months and was shocked when I came to know that JavaScript is a prototype-based language.

我经历了一些关于如何使用JavaScript的原型和构造函数的Stack&Overflow问题。

I went through some Stack Overflow questions about how JavaScript was supposed to be used and came across prototypes and constructors.

我学会了,现在我可以说不是一个noob当涉及到构造函数和原型。我熟悉的语法。但我仍然认为我错过了一些东西,并没有下到这种语言的核心,我有时会困惑。

I learned it, and now I can say that I am not a noob when it comes to constructors and prototypes. I am familiar with the syntax. But still I think I am missing something and haven't got down to the core of this language, and I do get confused sometimes.

我希望我很清楚。 / p>

I hope I am clear.

推荐答案


简单的英语中的构造函数和原型?

Constructor and protoypes in plain English?

构造函数创建对象并为其分配原型。原型是具有各种属性的对象,对象可以通过原型链继承。一如既往,示例帮助:

Constructor functions create objects and assign prototypes to them. A prototype is an object with various properties that an object can inherit through the prototype chain. As always, examples help:

function Foo() {
}
Foo.prototype.answer = 42;

var f = new Foo();
console.log(f.answer); // "42"

Foo 构造函数。当使用 new Foo 时, Foo.prototype 指向的对象将成为创建的对象的原型。当您执行 f.answer 时,由于 f 没有其自己的 名称 answer ,JavaScript引擎查看 f 的原型以查看 有一个。因为它是,它使用原型的值,我们在控制台中看到42。这是属性如何解决:通过查看一个对象,如果它有一个给定的名称的属性,如果没有,去它的原型,看看它是否有有属性,如果不去

Foo is a constructor function. When you use new Foo, the object that Foo.prototype points to will become the prototype of the object that gets created. When you do f.answer, since f doesn't have its own property with the name answer, the JavaScript engine looks at f's prototype to see if it has one. Since it does, it uses the value from the prototype and we see "42" in the console. This is how properties are resolved: By looking at an object seeing if it has a property with the given name, and if not, going to its prototype to see if it has the property, and if not going to its prototype, and so on.

注意,上面的结果是在之后添加属性到 em>一个对象已经创建使用那个原型工程很好;你可以通过对象使用这些新的属性:

Note that a consequence of the above is that adding properties to a prototype after an object has been created using that prototype works just fine; you can use those new properties via the object:

function Foo() {
}
Foo.prototype.answer = 42;

var f = new Foo();
console.log(f.question); // "undefined", neither `f`, nor `Foo.prototype`, nor
                         // `Object.prototype` has a `question` property

Foo.prototype.question = "Life, the Universe, and Everything";
console.log(f.question); // "Life, the Universe, and Everything"

从ES5开始,构造函数不再是只有这样才能将原型分配给对象。现在你也可以通过 Object.create 来做到。以上是大致

As of ES5, constructor functions are no longer the only way you can assign prototypes to objects. Now you can also do it via Object.create. The above is roughly equivalent to this:

var fooProto = {
    answer: 42
};
var f = Object.create(fooProto);
console.log(f.answer); // "42"




使用Prototypes和构造函数?

What is the purpose behind using Prototypes and constructors?

在对象之间共享特征。原型的属性可以是函数或数据,使用该原型的对象都可以访问和重用。

To share characteristics between objects. The properties of a prototype can be functions or data, both of which the objects using that prototype have access to and can reuse.

请在下面注释:


我理解了关于共享特征的部分,但我可以得到更多的细节。

I understood the part about sharing characteristics, but could I get some more detailing in to it

好吧,考虑一个 Circle 构造函数:

Well, consider a Circle constructor:

function Circle(radius) {
    this.r = radius;
}
Circle.prototype.radius = function() {
    return this.r;
};
Circle.prototype.diameter = function() {
    return this.r * 2;
};
Circle.prototype.circumference = function() {
    return 2 * Math.PI * this.r;
};
Circle.prototype.area = function() {
    return Math.PI * this.r * this.r;
};

Circle 构造的所有对象都将获得 Circle.prototype 作为他们的原型,所以他们都有方便 diameter circumference ,et。 et al。

All objects constructed by Circle will get Circle.prototype as their prototype, and so they all have the handy diameter, circumference, et. al. functions.

var c1 = new Circle(3);
console.log(c1.area());          // 28.274333882308138
console.log(c1.circumference()); // 18.84955592153876

var c2 = new Circle(5);
console.log(c2.area());          // 78.53981633974483
console.log(c2.circumference()); // 31.41592653589793

它们以高效的方式共享这些属性:每个实例没有这些属性的自身副本(这意味着保持每个属性名称及其在每个obejct中的值);相反,他们只是引用了他们的原型,他们分享,它有那些属性。

They share those properties in a memory-efficient way: Each instance doesn't have its own copy of those properties (which would mean keeping each property name and its value in each obejct); instead, they just have a reference to their prototype, which they share, which has those properties.

这篇关于JavaScript中的原型和构造函数(plain English)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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