Created
February 22, 2016 13:28
-
-
Save deepakkj/bc54147f54623e1ac179 to your computer and use it in GitHub Desktop.
Javascript - Namespace, Objects, Class, Constructors, Properties
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
<script type='text/javascript'>//<![CDATA[ | |
window.onload=function(){ | |
// create a global object namd myapp by checking if thre exists a another same object in the same file or another filw. | |
//It it exists then assign that object to the new object or else create a new object with empty methods, functions and variables. | |
//global namespace | |
var myapp = myapp || {}; | |
//sub-namespace | |
myapp.event = { | |
addGame: function(name, date){ | |
//code stuff | |
this.name = name; | |
this.date = date; | |
}, | |
removeGame: function(name){ | |
//code stuff | |
this.name = undefined; | |
this.date = undefined; | |
}, | |
getGame: function(){ | |
return (this.name); | |
} | |
} | |
myapp.event.addGame("Halo", "Feb, 2006"); | |
console.log(myapp.event.getGame()); | |
myapp.event.removeGame("Halo", "Feb, 2006"); | |
console.log(myapp.event.getGame()); | |
//defining a class called Hello by with an empty constructor | |
var Car =function(){}; | |
//Creating an object using the above class- Instantiation | |
var sedan = new Car(); | |
var suv = new Car(); | |
//constructor example | |
// a constructor is executed only once when an object is created | |
var Person = function(){ | |
console.log('Instance created'); | |
}; | |
var p1 = new Person(); | |
var p2 = new Person(); | |
//The property | |
//properties are variables in a class | |
//here, we define name as property for the LivingBeign class | |
var LivingBeing = function(name){ | |
this.name =name; | |
console.log('LivingBeing instantiated'); | |
}; | |
var human = new LivingBeing("Deepak"); | |
var cat = new LivingBeing("Tom"); | |
console.log("LivingBeing1 is " + human.name); | |
console.log("LivingBeing2 is " + cat.name); | |
}//]]> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment