<script src="https://searls.github.io/jasmine-all/jasmine-all-min.js"></script>
<h1>Constructor Functions</h1>
<ul>
<li><b>new</b> keyword is special</li>
<li>Every object has a <i>prototype</i></li>
</ul>
<h2>Exercises</h2>
<ul>
<li>Add <i>numberOfGears</i> to return 5 by default</li>
</ul>
JavaScript
function Vehicle(name, year) {
this.name = name;
this.year = year;
}
Vehicle.prototype.getName = function() {
return this.name;
}
Vehicle.prototype.toString = function() {
return this.name + " - " + this.year;
}
var santro = new Vehicle("Santro", 2001);
var swift = new Vehicle("Swift", 2013);
swift.toString = function() {
return this.year + " was when " + this.name + " was bought";
}
swift.isAutomatic = function() {
return false;
}
describe("Vehicle constructor", function() {
it("should have Santro as the name", function() {
expect(santro.toString()).toBe("Santro - 2001");
});
it("should have Vehicle as the constructor", function() {
expect(santro.constructor).toBe(Vehicle)
});
it("swift should have its owne toString function", function() {
expect(swift.toString()).toBe("2013 was when Swift was bought")
});
it("swift should have a isAutomatic function", function() {
expect(santro.isAutomatic).toBe(undefined);
expect(swift.isAutomatic()).toBeFalsy()
});
it("return the number of gears", function(){
expect(santro.numberOfGears).toBe(5);
expect(swift.numberOfGears).toBe(5);
})
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.