Skip to content

Instantly share code, notes, and snippets.

View t0dd's full-sized avatar

Todd Mahoney t0dd

  • NWRECC
  • Boise, Idaho
View GitHub Profile
@t0dd
t0dd / defineproperties
Last active December 17, 2015 05:49
JS - Using the defineProperty() option available in EcmaScript 5
//Example 1
var createPerson = function(firstName, lastName){
var person = {};
//Descriptor object - define single property
//2 types - Data & Accessor descriptors
Object.defineProperty(person, "firstName", {
@t0dd
t0dd / gist:5553293
Created May 10, 2013 08:56
The constructor pattern - JS OOP.
//The constructor pattern
function Robot(name, model, job){
this.name = name;
this.model = model;
this.job = job;
this.sayMyName = function() {
alert(this.name);
};
}
@t0dd
t0dd / FactoryPattern
Created May 10, 2013 08:39
The factory pattern. a pseudo "interface" for objects in JS.
//The Factory Pattern
var createPerson = function(firstName, lastName) {
return {
firstName : firstName,
lastName : lastName,
greet : function(){
return "Hey there " + firstName + " " + lastName;
}
};
};
@t0dd
t0dd / protoPerson
Created May 10, 2013 06:45
Just a simple demo of prototype chaining & inheritance I find useful.
function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
};
Person.prototype.getFullName = function(){
return this.firstName + " " + this.lastName;
};
function Employee(firstName, lastName, position){
@t0dd
t0dd / arraylist convert
Created October 30, 2012 20:19
converting a Java ArrayList to an int[] array
//converting a Java ArrayList to an int[] array
int[] arr = new int[myArrList.size()];
for (int i=0; i < arr.length; i++){
arr[i] = myArrList.get(i).intValue();
}
@t0dd
t0dd / Java custom ArrayList toString()
Created October 30, 2012 19:57
Java custom ArrayList toString()
//A custom toString method that returns a meaningful string representation of an ArrayList object
public String toString() {
String strOutput = "MyArrayListObject ["; //Prints constructor name + left bracket
for(int i = 0; i < myArr.size(); i++){ //myArr = ArrayList instance variable
strOutput += myArr.get(i) + ", ";
}
strOutput = strOutput + "]";
return strOutput;
@t0dd
t0dd / myPoint.java
Created October 20, 2012 17:48
Using the java.awt.Point class to set and return an (x,y) coordinate point.
import java.awt.Point;
public class Vehicle{
private Point location = new Point();
private String name;
public Vehicle(Point location, String name){
this.location = new Point(location); //Since Point object is mutable, it should be copied.
vehicleName = name;
}