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");
2 Antworten
+ 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 );
0
Thanks... it has worked...I have seen the errors