console.log('Program starts');
// we start defining the Person class
function Person() {
// this is a method of the class Person, it 'generates' a gender
this.decideGender = function() {
return (Math.random() > 0.5) ? 'boy' : 'girl';
}
// 'gender' is a property of the class
this.gender = this.decideGender();
// everything you execute directly here is the 'constructor', and it will execute as soon as you instantiate the class
console.log('Person instantiated. Congratulations! It\'s a ' + this.gender + '!');
}
var person1 = new Person();
var person2 = new Person();
// we define now the Student class
function Student() {
// 'decideSchool' is a method of the class Student
this.decideSchool = function() {
return (Math.random() > 0.5) ? 'Westside' : 'Eastside';
}
// here we call the constructor of the Parent class. Remeber this is the constructor of the Student class too.
Person.call(this);
this.school = this.decideSchool();
console.log('Student instantiated. Your little monster is now going to ' + this.school);
}
// now we can make the Student inherit Person
Student.prototype = Person;
// keep in mind that just that line makes the Student.prototype.constructor be the Person's
// because we don't want that, we correct it with this line
Student.prototype.constructor = Student;
var student1 = new Student();
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.
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!
Console
Debug your Fiddle with a minimal built-in JavaScript console.