Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save csdear/800eb1c2998612794c56 to your computer and use it in GitHub Desktop.
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…
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