简体   繁体   English

javascript中的原型

[英]prototype in javascript

I was looking into the prototype object, and i am a bit confused about the following 我正在研究原型对象,我对以下内容感到有些困惑

//my constructor function
function Circle(r) { 
    this.radius = r;
    this.PI = 3.14;
}

function areaCalculator() {
    this.area = this.PI * this.radius * this.radius;
}

 function circumferenceCalculator() {
    this.circumference = 2* this.PI * this.radius;
}

since our function is an object and has a property called prototype, it is possible to add properties and methods to this prototype object and these would automatically be available for all our custom objects we create using our function constructor. 因为我们的函数是一个对象,并且有一个名为prototype的属性,所以可以向这个原型对象添加属性和方法,这些对于我们使用函数构造函数创建的所有自定义对象都可以自动使用。

Circle.prototype.areaCalculator = areaCalculator; //adding function
Circle.prototype.color = "Red"; //adding property

var circle1 = new Circle(5);
circle1.areaCalculator();
console.log(circle1.radius);//5
console.log(circle1.area); //78.5
console.log(circle1.color);//Red

If I understand correctly, all objects using Circle will reference the same color variable unless they are explicitly set. 如果我理解正确,除非明确设置,否则使用Circle的所有对象都将引用相同的颜色变量。 Is this correct? 这个对吗?

Also what does it mean to do something like below without using prototype 在不使用原型的情况下,做下面的事情也意味着什么

Circle.circumferenceCalculator = circumferenceCalculator;
Circle.color = "Red";

Are the above two statements correct? 以上两个陈述是否正确?

Yes, all the objects created with new Circle will point to the same color property. 是的,使用new Circle创建的所有对象都将指向相同的color属性。 This property will actually be located on the prototype object, not on the object that you have created. 此属性实际上位于原型对象上,而不是您创建的对象上。 Thus, when you set it on a specific object, it will "shadow" the property from the prototype, but not overwrite it - you can try to delete obj.color after you set it and you'll get the old color back. 因此,当您在特定对象上设置它时,它将从原型“遮蔽”该属性,但不会覆盖它 - 您可以尝试在设置它之后delete obj.color ,然后您将获得旧颜色。

Doing Circle.color='red' will simply set the color property on the Circle object (even functions are objects, but they have a callable property which defines their behaviour when called) - this is in no way connected to the Circle 's prototype. 执行Circle.color='red'将简单地设置Circle对象的color属性(即使函数是对象,但它们具有callable属性,在调用时定义它们的行为) - 这与Circle的原型无关。

A function is also an object and you can augment properties to it. 函数也是一个对象,您可以为其添加属性。 A common example is jQuery, where the $ acts as both an object and a function. 一个常见的例子是jQuery,其中$充当对象和函数。

function Circle(r) {}
Circle.circumferenceCalculator = circumferenceCalculator;
Circle.color = "Red";

$.each();             //$ as an object
$('selector').each()  //$ as a function

However, this won't reflect in the instance you create. 但是,这不会反映在您创建的实例中。 The ones that reflect on the instances are only those that are added via prototype and those in the constructor function. 反映实例的那些只是那些通过原型和构造函数中添加的实例。

function Circle(r) { 
    this.radius = r;
    this.PI = 3.14;
}

Circle.prototype.areaCalculator = areaCalculator;
Circle.prototype.color = "Red";

var mycircle = new Circle(5);

//props on Circle itself:
//Circle.color
//Circle.areaCalculator

//props on the instance:
//mycircle.radius
//mycircle.PI
//mycircle.areaCalculator
//mycircle.color

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

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