Created
December 30, 2014 13:47
-
-
Save csdear/800eb1c2998612794c56 to your computer and use it in GitHub Desktop.
Create Javascript Object : new Notation and Constructor Functions • constructor function will be used as the blueprint when creating new instances with the new kw • Constructor function special b/c you can refer to an instance by using the this keyword. • Convention to use uppercase at the beginning of a constructor function -- e.g., Car. • Ea i…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Car(type) { | |
this.speed = 0; | |
this.type = type || "No type"; | |
this.drive = function(newSpeed) { | |
this.speed = newSpeed; | |
} | |
} | |
//creating a new instance of the Car object, the bmw. | |
var bmw =new Car("BMW"); | |
console.log(bmw.speed); // outputs 0 | |
console.log(bmw.type); // outputs "BMW" | |
bmw.drive(80); | |
console.log(bmw.speed); // outputs 80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment