Objects in JavaScript

Goal:

Use objects to add grass to our sketch, when completed, your sketch should show something like this:

Try it Here:

To start the project, click on the link below. Use the tutorial link and follow the instructions:

Objects Project

In Depth Guide

Topics:

  • for loops
  • Objects

    Details:

for loops

Earlier, we covered the control structure called a while loop. The while loop allows us to iterate until some condition is met. Another type of loop is called a for() loop. The for loop in languages that follow C-Style syntax (JavaScript does), is actually a shorthand for a while loop. Although you can do exactly the same thing in a for loop as you can in a while loop, it doesn't mean you should. In general, a for loop is for counting. If you want to do some task n times, a for loop is ideal. If you want to do something until a condition is met, a while loop is better. Following these rules will help you write cleaner code.

Object

JavaScript is an Object Oriented language. Even functions in JavaScript are objects, even though we don't always view it that way. However, JavaScript is not "classy", which is different from languages like Java and C++. In languages like C++ and Java, you create a class. The class itself is not an object. To create an object, you must instantiate the class. This instance of the class is an object. In JavaScript, you simply create an objects. You can inherit properties from other objects too. This can be a very different experience than how you write objects in "classy" OO languages like C++ and Java. However, this method is actually simpler. While there are different ways that you can create objects in JavaScript, one way is to use a function that can be called to create objects.

In classical languages we typically declare private data members that store information about each object. In JavaScript, properties of the object are completely public. One way that we can get privacy is to use the idea of a closure.

results matching ""

    No results matching ""