Skip to content

Instantly share code, notes, and snippets.

@nola
nola / eventhandlerfunction.js
Created November 26, 2013 02:23
jquery event handler + function
$("#big-button").click(showSomething)
function showSomething() {
alert ('You clicked me');
}
@nola
nola / eventhandlerfunction2.js
Created November 26, 2013 02:27
$(“#name”) tells jQuery to deal with element id=’name’. .keyup(updateName) calls jQuery keyup method and tells it to run updateName function whenever it detects keystroke on #name element. The first line of updateName() function makes use of jQuery val() method to read the current value of #name input. It will then store that value inside ‘name’…
$("#name").keyup(updateName)
function updateName() {
var name=$("#name").val();
$("#user-name").text(name);
}
@nola
nola / object-constructor.js
Last active December 29, 2015 12:28
1. Define the object type by writing a constructor FUNCTION. Use a capital letter. 2. You can create any number of Car objects by calls to new. For example, 3. mycar.year = 1993, and so on. 4. then pass in an object as a property 5. create some new Person objects 6. create some new Car objects. Pass objects rand and ken as the arguments for the …
//1
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
//Notice the use of this to assign values to the object's properties based on the values passed to the function.
}
//2
@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) {
@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 / 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 / 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 / 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 / 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 / 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!');
}
});