Created
August 14, 2015 07:01
-
-
Save Opener-Park/b3d3cef808969e9e7109 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
it("why need to set prototype property explicitly?", function() { | |
//define object | |
function Talent(name, category, majorTalent) { | |
this.name = name || ""; | |
this.category = category || "unknown"; | |
this.majorTalent = majorTalent || "none"; | |
} | |
function TalentedForMusic(name, category, majorTalent, skills) { | |
//base is not special property. Assign the constructor of Talent to base. | |
this.base = Talent; | |
this.base(name, category, majorTalent); | |
this.skills = skills || []; | |
} | |
function TalentedForPaint(name, category, majorTalent, skills) { | |
//base is not special property. Assign the constructor of Talent to base. | |
this.base = Talent; | |
this.base(name, category, majorTalent); | |
this.skills = skills || []; | |
} | |
TalentedForPaint.prototype = new Talent; | |
//create instances | |
var talented = new Talent("thinkhard.j.park", "engineering", "programming"); | |
expect(talented.name).toBe("thinkhard.j.park"); | |
expect(talented.category).toBe("engineering"); | |
expect(talented.majorTalent).toBe("programming"); | |
var hummingMan = new TalentedForMusic("humming man", "artist", "music", [""]); | |
expect(hummingMan.name).toBe("humming man"); | |
expect(hummingMan.category).toBe("artist"); | |
expect(hummingMan.majorTalent).toBe("music"); | |
expect(hummingMan.skills).toBeDefined(); | |
var paintingGirl = new TalentedForPaint("painting girl", "artist", "art", [""]); | |
expect(paintingGirl.name).toBe("painting girl"); | |
expect(paintingGirl.category).toBe("artist"); | |
expect(paintingGirl.majorTalent).toBe("art"); | |
expect(paintingGirl.skills).toBeDefined(); | |
Talent.prototype.grade = "none"; | |
expect(talented.grade).toBe("none"); | |
expect(hummingMan.grade).toBe(undefined); | |
expect(paintingGirl.grade).toBe("none"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment