Skip to content

Instantly share code, notes, and snippets.

View emkay's full-sized avatar

Michael Matuzak emkay

View GitHub Profile
@emkay
emkay / object_literal.js
Created November 14, 2011 00:08
Object Literal Example
var student = {
"name": "Bob Barker",
"age": 100
};
console.log(student.name);
console.log(student.age);
console.log(student["name"]);
@emkay
emkay / object_example.js
Created November 14, 2011 01:33
Object Example
var person = {
"name": "Bob Barker"
};
person.name = "Batman";
//or
person["name"] = "Batman";
@emkay
emkay / functions.js
Created November 14, 2011 02:13
Function Examples
// add
var add = function (a, b) {
return a + b;
};
// or
function add (a, b) {
return a + b;
}
@emkay
emkay / functions_as_methods.js
Created November 14, 2011 02:19
Functions as Methods
var counter = {
value: 0,
increment: function () {
this.value += 1;
},
reset: function () {
this.value = 0;
}
};
@emkay
emkay / functions_this_problem.js
Created November 14, 2011 02:49
Functions this Problem
var person = {
first_name: "Robert",
last_name: "Barker",
nick_name: "Bob",
full_name: undefined,
name_helper: function (short) {
var shorter_name = function () {
this.full_name = this.nick_name + " " + this.last_name;
};
@emkay
emkay / functions_this_solution.js
Created November 14, 2011 02:51
Functions this Solution
var person = {
first_name: "Robert",
last_name: "Barker",
nick_name: "Bob",
full_name: undefined,
name_helper: function (short) {
var that = this; // the important part
var shorter_name = function () {
// this to that
that.full_name = that.nick_name + " " + that.last_name;
@emkay
emkay / function_scope.js
Created November 14, 2011 03:44
Function Scope
var a = 100,
b = 0;
function change() {
var b = a + 5;
}
change();
alert(b);
@emkay
emkay / array_examples.js
Created November 14, 2011 04:02
Array Examples
var people = ['Bob', 'Sally', 'Sara', 'Frank'];
console.log(people.length); // 4
var i;
var len = people.length;
for (i = 0; i < len; i += 1) {
console.log(people[i]);
}
@emkay
emkay / array_methods.js
Created November 14, 2011 04:33
Array Methods
var a = []; // empty array
a.push('a', 'b', 'c', 'd', 'e'); // ["a", "b", "c", "d", "e"]
a.pop(); // ["a", "b", "c", "d"]
a.reverse(); // ["d", "c", "b", "a"]
var s = a.join('');
@emkay
emkay / ajax.js
Created November 17, 2011 19:30
Ajax Example
var button, button2, button3;
var i, xhr, activeXids = [
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'
];
if (typeof XMLHttpRequest === "function") {
xhr = new XMLHttpRequest();
} else {