Skip to content

Instantly share code, notes, and snippets.

@nola
nola / extension-example.js
Last active January 2, 2016 03:49
jquery extension example. Notice the use of chaining the styles together
// creates a function that will add a 2px blue border around each div with the class "blue"
$.fn.blueBorder = function(){
this.each(function(){
$(this).css("border","solid blue 2px");
});
return this;
};
//creates a function that will turn the text color blue of each div
$.fn.blueText = function(){
// this targets Google Chrome and Opera 18+
var isChromium = window.chrome;
if(isChrome === true) {
// is chromium based browser
} else {
// not chromium based browser
}
//To target if the browser is Google Chrome, use this:
var isChromium = window.chrome,
@nola
nola / objects-as-parameter.js
Last active December 30, 2015 20:39
Passing objects to functions.If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as a parameter, and the function changes the object's properties, that change is visible outside the function, as shown in the following example:
var myFunc;
var num = 0;
if (num === 0){
myFunc = function(theObject) {
theObject.make = "Toyota";
theObject.year = "2003";
return theObject;
};
}
@nola
nola / no-name-function.js
Created December 9, 2013 22:28
stores the function in the variable "square" so you call square()
var square = function(number) {return number * number};
var x = square(4) //x gets the value 16
@nola
nola / monitor.js
Last active December 30, 2015 20:29
Monitor events on the DOM
//put this in the console
monitorEvents(window)
//only mouse events
monitorEvents(window, 'mouse')
//only keyboard events
monitorEvents(window, 'key')
//only scroll activity
@nola
nola / on-event.js
Last active December 30, 2015 19:49
jquery .on with event. New and Old way with bind
$('body').on({
click: function() {
event.preventDefault();
console.log('item anchor clicked');
},
mouseenter: function() {
console.log('enter!');
}
});
@nola
nola / in.js
Created December 1, 2013 04:21
propNameOrNumber in objectName
// Arrays
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees; // returns true
3 in trees; // returns true
6 in trees; // returns false
"bay" in trees; // returns false (you must specify the index number,
// not the value at that index)
"length" in trees; // returns true (length is an Array property)
// Predefined objects
@nola
nola / delete.js
Last active December 29, 2015 20:19
//The delete operator deletes an object, an object's property, or an element at a specified index in an array. Syntax:
delete objectName;
delete objectName.property;
delete objectName[index];
delete property; // legal only within a with statement
//If the delete operator succeeds, it sets the property or element to undefined.
//The delete operator returns true if the operation is possible; it returns false if the operation is not possible.
x = 42;
@nola
nola / shortif.js
Created November 30, 2013 18:40
short hand if statement.
var age = 19;
if(age >= 18){
var status = "adult";
}else{
var status = "minor";
}
//short hand version
var status = (age >= 18) ? "adult" : "minor";
@nola
nola / if-true-createObject.js
Created November 28, 2013 03:54
The following statement creates an object and assigns it to the variable x if and only if the expression cond is true:
if (cond) var x = {hi: "there"};