Last active
October 5, 2015 21:38
-
-
Save chhantyal/2881476 to your computer and use it in GitHub Desktop.
Concept of OOP in Javascript
This file contains 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
function Subject(name, topics){ | |
this.name = name; | |
this.topics = topics; | |
} | |
var subjectMath = new Subject("Maths", ["sets", "Trigonometry", "algebra"]); | |
var subjectScience = new Subject("Science", ["Biology", "Astronomy", "Physics"]); | |
function Person(name, gender, age, currentSubject, currentTopicIndex){ | |
this.name = name; | |
this.gender = gender; | |
this.age = age; | |
this.currentSubject = currentSubject; | |
this.currentTopicIndex = currentTopicIndex; | |
this.currentTopicName = this.currentSubject.topics[currentTopicIndex]; | |
//this.currentTopicName = this.currentSubject.topics[currentTopicIndex + 1]; | |
} | |
Person.prototype.describe = function () { | |
console.log(this.name +" is " + this.gender + | |
", age " + this.age + " and reading " + | |
this.currentSubject.name + ' with topic ' + | |
this.currentTopicName); | |
}; | |
Person.prototype.hello = function(){ | |
console.log(this.name + " says Hi"); | |
}; | |
Person.prototype.goToNextTopic = function () { | |
this.currentTopicIndex = this.currentTopicIndex +1; | |
this.currentTopicName = this.currentSubject.topics[this.currentTopicIndex]; | |
}; | |
var person1 = new Person("John", "Male", 21, subjectMath, 0); | |
var person2 = new Person("Rita", "Female", 25,subjectMath, 0); | |
person1.describe(); | |
person1.hello(); | |
person1.goToNextTopic(); | |
person1.describe(); | |
person1.goToNextTopic(); | |
person1.describe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment