Last active
August 29, 2015 14:06
-
-
Save ezekielriva/35e96646fd76d1f59e4e to your computer and use it in GitHub Desktop.
[PART 1] OOP Javascript. Object Definition.
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
/* | |
* Hi, Everybody. | |
* I'm Ezequiel and I'm going to teach you about JS Objects. | |
* | |
* Please note that I'll skip the theory, so anyone reading this gists must have | |
* experience on OOP JS. | |
* | |
* First of all, we must use auto-called JS functions to define a Class. | |
* If you don't know what an auto-called function is, please read about it in | |
* Mozilla Development Network. | |
* | |
* Our case of study: | |
* The Person class is very simple. It has two attributes or properties called | |
* name and age and some weird methods. | |
* | |
*/ | |
/* | |
* We must define our class using the syntax below: | |
* var Class = (function(Dependencies separated by a comma){ | |
* //code | |
* })(Dependencies separated by a comma) | |
* | |
* Using Camelcase notation is a good practice to define Class names. | |
*/ | |
var Person = (function () { | |
/* | |
* Inside the auto-called function you must define the Constructor function, | |
* private methods and private constant variables. | |
* I recommend starting with Private Constant variables. | |
*/ | |
/* | |
* Here is an example of Private Constant Variable. | |
* Using UPPERCASE and Snake_Case notation is a good practice. | |
*/ | |
const AGE_LIMIT = 77; | |
/* | |
* Private functions must be defined using "Function Declaration" syntax. | |
* Note: If you don't know what "Function Declaration" is, check MDN doc. | |
* | |
* It's important to know the following: | |
* - You can only access to private variables using getter and setters methods. | |
* - Sometimes you can use [this] statement inside private functions, but when | |
* you call them you must use apply or call method. | |
* - You may define here only the private methods that you want to use in | |
* Person Prototype. | |
*/ | |
function makeOlder () { | |
var newAge = this.getAge() + 1; | |
this.setAge( newAge ); | |
return newAge; | |
} | |
function dieAlert () { | |
alert(this.dieMessage + '. At age: ' + this.getAge()); | |
} | |
function aliveAlert () { | |
alert(this.aliveMessage + '. At age: ' + this.getAge()); | |
} | |
/* | |
* After defining private methods, you may define the Class constructor using | |
* the syntax below: | |
* function Class (arguments) { | |
* // code | |
* } | |
*/ | |
function Person (name, age) { | |
/* | |
* Start defining private variables. | |
* It's a good practice to define this variable using Underscore as prefix. | |
* Use [var] statement to define private variables. | |
*/ | |
var _name, | |
_age, | |
__proto__ = Person.prototype | |
; | |
_name = name; | |
_age = age; | |
/* | |
* Another kind of private methods might be defined here. | |
* The difference between these methods and those defined above is that | |
* these methods have direct access to private variables and methods. Also, | |
* they may not be used in Prototype Functions defined outside this | |
* constructor. | |
* They don't need to be called using call or apply methods if they have | |
* inside [this] statements. Instead, they must use the bind method. | |
* | |
* Check the follow syntax: | |
*/ | |
var log = function () { | |
console.log('Name:',_name); | |
}.bind(this); | |
/* | |
* You may define public variables inside Constructor. | |
* Check the following syntax: | |
*/ | |
this.aliveMessage = 'You are alive!'; | |
/* | |
* Also, you may define Privileged method. These methods are like Public | |
* Methods | |
* IMPORTANT NOTE: | |
* - Each time that you instance this Class these methods are cloned, so they | |
* use a lot of memory to allocate these methods. But these ones are | |
* faster than Prototype methods. | |
* Use your criteria to use these methods or Prototype methods. | |
* | |
* Check the following syntax: | |
*/ | |
this.isDied = function () { | |
var are = ( AGE_LIMIT <= _age )? dieAlert : aliveAlert; | |
log(); | |
return are.apply(this); | |
} | |
this.birthday = function () { | |
return makeOlder.apply(this); | |
} | |
/* | |
* These are another kind of methods. The Public methods. | |
* Here is a little difference between these methods and the another ones | |
* defined outside of Constructor. These have direct access to private | |
* methods and variables defined inside of Constructor. | |
* IMPORTANT NOTE: | |
* - These methods are shared between different instances of Class. So these | |
* use less memory than Privileged methods, but are slower. | |
* | |
* Use your criteria to use these methods or Privileged methods. | |
* | |
* Check the following syntax: | |
*/ | |
__proto__.setAge = function (age) { _age = age; } | |
__proto__.setName = function (name) { _name = name; } | |
__proto__.getName = function () { return _name; } | |
__proto__.getAge = function () { return _age; } | |
} | |
/* | |
* You may define Prototype variables. These are shared between every | |
* instances of Class | |
* | |
* Check the following syntax: | |
*/ | |
Person.prototype.dieMessage = "You are died!"; | |
/* | |
* This is another way to define Prototype Methods with the difference that | |
* they don't have direct access to private variables. They can only access | |
* private variables using getter and setter methods. | |
*/ | |
Person.prototype.makeHappy = function() { | |
alert("I'm happy :) !"); | |
} | |
/* | |
* Using this way you can define static variables or functions. | |
* Anyone can read and write these variables. | |
* Sometimes they are called Class Variables or Class Methods. | |
*/ | |
Person.staticVariable = 'some value'; | |
Person.alert = function (message) { | |
alert(message); | |
} | |
// At the end of auto-called function, you must return your Class | |
return Person; | |
})(); | |
/* | |
* Thank you for read! | |
* You can follow me on twitter (@ezekielriva) and github (ezekielriva). | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment