当前位置: 动力学知识库 > 问答 > 编程问答 >

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";

};

网友答案:

Prototype allows you add new properties and correct way of doing that will be:

person.prototype.hair_color = "brown";
分享给朋友:
您可能感兴趣的文章:
随机阅读: