Skip to content

Instantly share code, notes, and snippets.

@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 / two-objs.js
Last active December 29, 2015 14:49
The following example creates myHonda with three properties. Note that the engine property is also an object with its own properties.
var myHonda = {color: "red", wheels: 4, engine: {cylinders: 4, size: 2.2}};
//console edits and results
myHonda.color
"red"
myHonda.engine
Object {cylinders: 4, size: 2.2}
myHonda.engine.size
2.2
myHonda.engine.size = "3.3"
@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"};
@nola
nola / for-in.js
Last active December 29, 2015 14:49
Enumerating over all the properties in an object. Find out whats in there.
//create the object with properties & values.
var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
//then LOOP(enumerate) through all the properties of the object myCar
function showProps(obj, objName) {
var result = "";
for (var i in obj) {