Skip to content

Instantly share code, notes, and snippets.

View pedromcunha's full-sized avatar
🤘
Hello! Olá!

Pedro Cunha pedromcunha

🤘
Hello! Olá!
View GitHub Profile
@pedromcunha
pedromcunha / gist:c420b5b22b7d655569ac
Created August 9, 2014 05:27
Modular Design Pattern : Augmenting your module
CAVESOFCLARITY = function (caves) {
var sandScript = "";
caves.setSandScript = function (message) {
sandScript = message;
};
return caves;
}(CAVESOFCLARITY);
//Notice how we are passing the CAVESOFCLARITY module, which was recently created in another code sample.
@pedromcunha
pedromcunha / gist:b6a1b6dc20b04fdbde94
Created August 9, 2014 04:28
Modular Pattern - Importing Global Variables when using closures.
var year = 2014;
var CLASSLIST = function (classYear){
var maleStudents = 15,
femaleStudents = 20;
return {
yearBookTitle: function () {
return "Congradulations year of " +
classYear +
var CLASSLIST = function (){
var maleStudents = 15,
femaleStudents = 20;
return {
amountOfStudents: function () {
return maleStudents + femaleStudents;
},
CURRICULUM: {
classStart: "September 15th 2014",
@pedromcunha
pedromcunha / gist:64b9dee5ca71be4326ac
Created August 9, 2014 03:30
Namespaces : Modularity Design Pattern
var TARRYTOWN = {
knights: 100,
citizens: 500,
stapleFood: "mutton chops",
alertTheLord: function() {
alert("We need help my lord, please send reinforcements!")
},
KINGSKEEP: {
calvary: 25,
knights: 200,
var passengers = [ ["Thomas", "Meeks"],
["Gregg", "Pollack"],
["Christine", "Wong"],
["Dan", "McGaw"] ];
var modifiedNames = passengers.map(
function (cell) {
return cell[0] + ' ' + cell[1];
}
);
var list = document.getElementById('friendsList'),
friends = ["Andre",
"Pedro",
"Johnny",
"Fer",
"Tiago",
"Danny"],
fragment = document.createDocumentFragment(),
element;
for(var i = 0, l = friends.length; i<l; i++) {
@pedromcunha
pedromcunha / gist:791c80f3ab800ccfb49f
Last active August 29, 2015 14:04
If/Else Case Switch Conditionals
function sansTemple(floor){
var happensNext;
switch(floor) {
//Notice how you can use a stacking method to reduce redundency in cases. Now lower level also works for the basement cond.
case "lower level" :
case "basement" :
welcomeMessage = "Welcome to the basement!";
case 1 :
welcomeMessage = "You have entered the First Floor!";
break
@pedromcunha
pedromcunha / gist:591d1ed814c3f0d562a1
Created August 2, 2014 19:32
OOP Javascript: Inheritance using constructors
function Tool(name, type, use, cost) {
this.name = name;
this.type = type;
this.use = use;
this.cost = cost;
//You can also add inherited functions here for everything created using this constructor
this.useTool = function () {
alert("Alright, be careful with that "+this.name+"!");
}
}
@pedromcunha
pedromcunha / gist:aed804e428a5dfe1635b
Created August 2, 2014 19:13
Prototypal Methods: Creating a new inherited method in the String prototype
String.prototype.checkFor = function (input) {
var result = 0;
for (var i = 0; i<this.length; i++) {//use this to check for the length of the actual string
this.charAt(i) == input ? result++ : false; //we can use a ternary conditional here.
}
return result;
};
console.log("This is a good string".checkFor("o")); //Now we can use the new method on a string and log it out.
@pedromcunha
pedromcunha / gist:b03261b10089a11abc3e
Created August 2, 2014 17:36
OOP JavaScript: Adding or removing objects via built in functions.
var fridge = {
addItem: function(name, type, expirationDate, healthRating) {
this[name] = {type: type, expirationDate: expirationDate, healthRating: healthRating};
},
removeItem: function(name){
var recentlyRemoved = delete this[name];
}
};
fridge.addItem("bannana", "fruit", "2 weeks", "100");
fridge.addItem("steak", "meat", "1 weeks", "70");