Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construction #3

Open
3 tasks
jugglinmike opened this issue Sep 25, 2014 · 0 comments
Open
3 tasks

Construction #3

jugglinmike opened this issue Sep 25, 2014 · 0 comments

Comments

@jugglinmike
Copy link
Collaborator

Lets define a JavaScript "class" to describe a Mole. This will hold all the
information (and code) required for a Mole to be a Mole. It won't do much at
first, but we'll add to it as we go.

  • mole.js (new file)
    • Create a JavaScript constructor named Mole that defines an el
      property as a DOM element
  • main.js (new file)
    • Create an instance of this class and append its element to the DOM
  • Stylesheet(s)
    • Define some styling for the element so that you can actually see it.
      This can be minimal (just a width, height, and background-color) or
      you can apply your designer powers and provide a sketch.

A class lets us define a kind of "template" or "mold" for creating custom
objects. We make a class by defining a "constructor" and (optionally) attaching
functions to its "prototype" (we'll work with the prototype later).

In JavaScript, a constructor is just a function that you invoke with new.
Inside the constructor, this refers to the "instance", or the the thing being
created.

// This is a "Car constructor".
function Car() {
  // We call `wheels` an "instance property"
  this.wheels = 4;
}
// later, we can use the constructor to make as many cars as we want. Here,
// we'll create two Car "instances":
var firstCar = new Car();
var secondCar = new Car();

// each "instance" has its own `wheels` property, since we defined it in the
// constructor
console.log(firstCar.wheels);
console.log(secondCar.wheels);
@jugglinmike jugglinmike modified the milestone: The First Mole Sep 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant