Last active
December 23, 2015 04:29
-
-
Save sean-roberts/6580320 to your computer and use it in GitHub Desktop.
This is a few of the ways to create an object 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
/* | |
The different ways to create an object | |
*/ | |
/* | |
Example object needs to have this heirachy | |
Bank Account | |
> holder - the account holder | |
> deposit - function allowing increase of balance | |
the input amount should be a positive number | |
> withdraw - function allowing decrease / use of balance | |
the input amount should be a negative number | |
> balance - if possible, a function allowing the view of the immutable balance | |
otherwise a int member for uing deposit and withdraw | |
note: the balance is immutable so it can not be directly set. | |
only through the use of deposit and withdraw can the balance change | |
-- Yes, technically we should be formatting the string to be in currency format, | |
but for the sake of the example being about objects, we will omit the formatting | |
-- Proper error throwing and input validation | |
have mostly been omitted for example sake as well | |
*/ | |
/**************************************** | |
1. Object Literals / Static Objects | |
a. They do not need instantiation with new operator | |
b. All members are public and can not share private vars | |
between one another | |
c. using {}, new Object(), or Object.create(Object.prototype) will | |
all create an empty object. | |
****************************************/ | |
var bankAccount = { | |
holder: '', | |
//we can't have a private value shared across members so | |
//we will have to throw the value into a public member | |
//this is the best we can do with object literal style of doing things | |
balance: 0, | |
withdraw: function (amount) { | |
//we need this amount to be less than 0 | |
amount = amount > 0 ? 0 : amount; | |
return this.balance += amount; | |
}, | |
deposit: function (amount) { | |
//we need this amount to be greater than 0 | |
amount = amount < 0 ? 0 : amount; | |
return this.balance += amount; | |
} | |
}; | |
var myAccount = bankAccount; | |
myAccount.holder = 'sean'; | |
/* | |
This object is static in the since that when you assign something | |
the BankAccount object you are assigning a reference this object. | |
So any mutation done upon any reference of this object will be reflected | |
through out the space and affect all other referencing objects. | |
Such classes are often called a singleton. This is so because at any given | |
time you only have one instance of the class. | |
*/ | |
/**************************************** | |
2. Constructed Objects / Instantiated Objects | |
a. They need instantiation with new operator | |
b. With constructors being functions, they have the benefit of | |
lexical scoping and closures allowing for private vars inside | |
of that scope | |
****************************************/ | |
var BankAccount = function (options) { | |
'use strict'; | |
//make sure the options is defined so | |
//sniffing for members isn't an exception | |
options = options || {}; | |
//immutable balance value | |
var _balance = 0; | |
return { | |
holder: options.holder || '', | |
balance: function () { | |
return _balance; | |
}, | |
withdraw: function (amount) { | |
//we need this amount to be less than 0 | |
amount = amount > 0 ? 0 : amount; | |
return _balance += amount; | |
}, | |
deposit: function (amount) { | |
//we need this amount to be greater than 0 | |
amount = amount < 0 ? 0 : amount; | |
return _balance += amount; | |
} | |
}; | |
}; | |
var myAccount = new BankAccount({ | |
holder: 'sean' | |
}); | |
/* | |
I prefer to return an object but you can just use 'this.withdraw = function...' | |
inside the body of the constructor. | |
note: the naming convention for classes defined with constructors | |
is it should be PascalCase. As opposed to literals that are operating | |
directly on an object so they should follow the camelCase style. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment