简体   繁体   English

使用Javascript将参数发送到原型

[英]Sending parameters to a prototype in Javascript

Is it possible to send a parameter to a prototype to set new properties of an object? 是否可以将参数发送到原型以设置对象的新属性? for example: 例如:

function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}

person.prototype = function(color) {
    this.hair_color = color;
};

This may not have been implemented correctly, but my question is still the same. 这可能没有正确实现,但是我的问题仍然是一样的。 I am trying to make sure I understand prototypes so maybe I am missing a fundamental idea about them. 我试图确保我了解原型,所以也许我错过了关于原型的基本概念。 It seems that everywhere I look prototypes are used to set defined variables as opposed to one that is input such as this 似乎到处都是原型,用来设置已定义的变量,而不是像这样输入的变量

person.prototype = function() {
    this.hair_color = "brown";
};

原型允许您添加新属性,正确的实现方式是:

person.prototype.hair_color = "brown";

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

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