PencilCoder

Custom(ized) Objects!

Goals:

  • Create objects using object literal notation
  • Modify existing objects using dot-notation

Coding Snippets:

  • coding snippets
  • Custom objects can be created using the Object constructor:  mars = new Object()
  • A simpler alternative is to use object literal notation (using curly braces) which also allows you to define properties when creating an object, as illustrated on the left.
  • Add, modify, or reference an object's properties using dot notation. This works for custom as well as built-in objects, such as Turtles.

Math and Computer Concepts:

  • Objects are made up of properties consisting of name/value pairs. The name is a variable name. The value can be data of any type: Number, Boolean, String, or even Object.
  • Coding ExampleObjects permit us to make a single unit out of data that belong together. We can exploit this feature to keep programs organized, as illustrated in this Solar System program, which uses separate objects to track information about each planet.
  • Being able to lump multiple pieces of information together in a single unit is also useful for passing around information within a program, such as to and from functions.

Activities:

Save each of the following in a folder called Objects.