Adding new value to prototype not working

Hi all!
Learning object prototypes right now and want to add a new value to my prototype, which already has 3 properties. codepen


Then I am creating a new object that shall use and assig values to all 4 propertys, in total 4.

let penguin = new animal(“Ture”, 5, 10, 222);

When I want to output the new last property it is not working, I get an “undefined”.


Other 3 properties are working.

Hm, so the enhanced template can not be used with all 4 properties including the new one?

Thank u so much! =]

That is not what a prototype is. You seem to be conflating prototype and constructor function parameters/arguments.

 function animal(name, age, velocity) { = name;
     this.age = age;
     this.fastness = velocity 
let penguin = new animal("Ture", 5, 10, 222);

If you call the constructor function with 4 arguments the constructor function definition should have 4 parameters, not 3.

If you assigned the prototype property with a value you would see it. Again this has nothing to do with the constructor function parameters/arguments.

animal.prototype.newproperty = 'some value';

I would also suggest you use classes instead of constructor functions unless you have a good reason not to.

Hi lasjorg!
Thank you for your extensive explanation. I guess I did not get the purpose of the prototype method correctly. You can just add a fixed key-value-pair to a template and all its future generated objects, but that´s it.
I thought I can add another flexible property with a parameter delivered from outside, but this is not working.
I highly appreciate your help and have a nice day! =]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.