Created
October 26, 2014 06:31
-
-
Save nirvanagit/10a7be2374cebfbcdda1 to your computer and use it in GitHub Desktop.
This is a program which tells you that JavaScript is a beautiful language
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
var biodata = function(firstName, secondName){ | |
//Public members:: | |
this.firstName = firstName; | |
this.secondName = secondName; | |
var that = this; | |
//private members: | |
var sex = "Male"; | |
var relationshipStatus = "in a relationship"; | |
//private methods: | |
var getSex = function(){ | |
console.log("Person " + that.firstName + " is a " + sex); | |
} | |
getSex(); | |
//This is a privileged function which is accessible by the outside world, but it itself has access to the private | |
//methods and variables. | |
//For instance, sex and relationshipStatus are private variable of the function biodata. | |
//getAllInfo (privileged function) provides access to the outside world with all these values | |
this.getAllInfo = function(){ | |
var allInfo = {}; | |
allInfo.firstName = this.firstName; | |
allInfo.secondName = this.secondName; | |
allInfo.sex = sex; | |
allInfo.relationshipStatus = relationshipStatus; | |
return allInfo; | |
} | |
} | |
//public method in prototype | |
biodata.prototype.collegeInfo = function(collegeName){ | |
this.collegeName = collegeName; | |
console.log("Person:" + this.firstName + " studied in " + this.collegeName); | |
} | |
var obj = new biodata("Sylvestor", "Stallone"); | |
obj.collegeInfo("University of Miami"); | |
var hackInfo = obj.getAllInfo(); | |
console.log("FirstName:", hackInfo.firstName); | |
console.log("secondName:", hackInfo.secondName); | |
//The privileged method provides access to private variables of a function | |
console.log("sex:", hackInfo.sex); | |
console.log("relationshipStatus:", hackInfo.relationshipStatus); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment