You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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".functionCar(){// 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":varfirstCar=newCar();varsecondCar=newCar();// each "instance" has its own `wheels` property, since we defined it in the// constructorconsole.log(firstCar.wheels);console.log(secondCar.wheels);
The text was updated successfully, but these errors were encountered:
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)Mole
that defines anel
property as a DOM element
main.js
(new file)This can be minimal (just a
width
,height
, andbackground-color
) oryou 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 beingcreated.
The text was updated successfully, but these errors were encountered: