Skip to content

Instantly share code, notes, and snippets.

@sydlawrence
Created February 8, 2011 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sydlawrence/817379 to your computer and use it in GitHub Desktop.
Save sydlawrence/817379 to your computer and use it in GitHub Desktop.
function Object() {
this.runMethod = function(callback) {
var data = "world";
callback(data);
}
}
var object = new Object();
function alertData(data) {
alert(data);
}
object.runMethod(alertData);
/*
This is to accompany the DMD12 module
*/
// standard variables
var foo = “bar”;
// when the window is ready (event listener)
document.onready = function(event) {
/* do something */
}
// define a method
function foo(bar) {
alert(bar);
}
// boolean expressions
if (foo == "bar") {
/* do something */
}
// array
var arr = ['a','b','c','d'];
// associative array
var assoc = {
name: 'syd',
email: 'syd@sydlawrence.com'
};
// object
var obj = {
name: 'syd',
email: 'syd@sydlawrence.com'
};
// for loops
for (i in assoc) {
/* do something with assoc[i]; */
console.log(assoc[i]);
}
// retrieving an element by id i.e. <div id='element'></div>
var el = document.getElementById('element');
// retrieving an element by css selectors i.e. <div class='element'></div>
var el = document.querySelector('.element');
/*
* DEBUGGING
*/
// similar to actionscript's trace method
console.log(obj);
// try catch
try {
/* do something */
}
catch(e) {
/* an exception has been caught */
}
/*
* MODIFYING THE DOM ELEMENTS
*/
el.style.background = '#00ffff';
el.style.position = 'absolute';
el.style.backgroundColor = '#00ff00';
/*
* EVENTS
*/
// when document is ready
document.onready = function(e) {}
// return false to intercept default action
el.onclick = function(e) {}
/*
Avoid putting an onclick event on anything other than an <a/> or a form item
There are exceptions
*/
// when an element gains focus
el.onfocus = function(e) {}
// when an element loses focus
el.onblur = function(e) {}
/*
There are many more, take a look here:
*/
/* javascript object sample */
function Person() {
this.first_name = "Syd";
this.last_name = "Lawrence";
this.email = "syd@sydlawrence.com";
this.fullName = function() {
return this.first_name + " " + this.last_name;
}
}
var person = new Person();
var person2 = new Person();
// rename the first person
person.first_name = "Bob";
// this will return "Bob Lawrence";
person.fullName();
// this will return "Syd Lawrence";
person2.fullName();
//First, create the custom object "circle"
function circle(){
}
circle.prototype.pi=3.14159;
// create the object method
circle.prototype.alertpi = function(){
alert(this.pi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment