Creating your own objects | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Creating your own objects

I am trying to create my own fruit object but when I run it, it is not displaying the output. What could the error be. here is my code: function fruit (fname, fcolor, forigin) { this.name=fname; this.color=fcolor; this.origin=forigin; } var apple= new fruit("apple", "red", "China"); var orange= new fruit ("orange", "orange", "Canada"); document.write("apple.fcolor"); document.write("orange.forigin");

13th Dec 2017, 7:33 AM
Stacy Kendi
2 Answers
+ 5
You have two errors in the last two lines. First you are defining the attributes of the object as "name", "color", "origin" and then you are trying to access them with the name of the parameters that you passed to the function ("fcolor", "forigin"). The second error is that you are actually printing a string ("apple.fcolor", "orange.forigin") and not the object with the property itself The last two lines should look like this: document.write(apple.color ); document.write(orange.origin );
13th Dec 2017, 7:44 AM
Mickel
Mickel - avatar
0
Thanks... it has worked...I have seen the errors
13th Dec 2017, 7:46 AM
Stacy Kendi