Skip to content

Instantly share code, notes, and snippets.

@chhantyal
Created June 6, 2012 11:05
Show Gist options
  • Save chhantyal/2881294 to your computer and use it in GitHub Desktop.
Save chhantyal/2881294 to your computer and use it in GitHub Desktop.
JS OOP
function Subject(name, topics){
this.name = name;
this.topics = topics;
}
var subjectMath = new Subject("Maths", ["sets", "trigonomitry", "algebra"]);
var subjectScience = new Subject("Science", ["Biology", "Astronomy", "Physics"]);
function Person(gender, age, currentSubject, currentTopicIndex){
this.gender = gender;
this.age = age;
this.currentSubject = currentSubject;
this.currentTopicIndex = currentTopicIndex;
this.currentTopicName = this.currentSubject.topics[currentTopicIndex];
}
Person.prototype.describe = function () {
console.log("person is " + this.gender +
", age " + this.age + " and reading " +
this.currentSubject.name + ' with topic ' +
this.currentTopicName);
}
var person1 = new Person("Male", 21, subjectMath,1);
var person2 = new Person("Female", 25,subjectMath,0);
person1.describe();
person2.describe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment