Prototypical Pattern Javascript

var Car = function(config) { this.carName = config.carName; this.gears = config.gears || 5; this.driving = false; } Car.prototype.drive = function() { this.driving = true; }; Car.prototype.getStatus = function() { console.log('Car:', this.carName, 'is currently', this.driving ? 'driving' : 'not driving'); } var myCar = new Car({ carName: 'SuperCar' });; myCar.drive(); myCar.getStatus(); // Car: SuperCar is currently driving

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.