Object as "Class"

by Jennifer Piccione

JavaScript

/*
ways to make objects function kinda like a "class"
*/

/*
Example 1
*/
function Car(color) {
    this.color = color;
    this.toString = function() {
        return this.color + " car";
    };
};

var toyota = new Car("red");

console.log(toyota.toString());

/*
Example 2
*/
function Car2(color) {
    this.color = color;
};

Car2.prototype.toString = function() {
    return this.color + " car";
};

var ford = new Car("red");
console.log(ford.__proto__ === Car.prototype);
console.log(ford.constructor === Car);