Skip to content

Instantly share code, notes, and snippets.

@nirvanagit
Created October 26, 2014 06:34
Show Gist options
  • Save nirvanagit/5ba769025c7685b67aaa to your computer and use it in GitHub Desktop.
Save nirvanagit/5ba769025c7685b67aaa to your computer and use it in GitHub Desktop.
This is a program which tells you that JavaScript is a beautiful language
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