July 15, 2024

JavaScript Objects


This question tests your knowledge of JavaScript objects and methods.

  • How to write a constructor
  • How to implement a method
  • How to instantiate an object
  • How to use a method

Hopefully you will realise that this approach to storing and accessing data is an improvement on the data structure presented in Q9.

You are required to model a mountain with an object. You will need to write a constructor, which takes two explicit arguments. Mountain will require two properties, name and height.
Write JavaScript for the constructor, instantiate a Mountain object and initialize the properties of this object, which represents Mount Everest (see output below for values).
Now implement a method toString(), which gets the values of the name and height properties of a Mountain object and outputs them in an alert box. Test this method on the object you created.

Output from using the toString() method - The name of this mountain is Everest (new line). The height is 8850 metres


Link to answer.


Next page » Event-driven programming

Previous page « Arrays








Up to top of page