Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / eventhandlerfunction.js
Created November 26, 2013 02:23
jquery event handler + function
$("#big-button").click(showSomething)
function showSomething() {
alert ('You clicked me');
}
@nola
nola / gist:7652479
Created November 26, 2013 02:12
Shorter IF’z If you have multiple IF variable value comparisons you can simply ass them to an array and check for presence. You could use $.inArray as an alternative.
if( myvar==1 || myvar==5 || myvar==7 || myvar==22 ) alert('yeah')
//shorthand
if([1,5,7,22].indexOf(myvar)!=-1) alert('yeah baby!')
@nola
nola / negativewhile.js
Created November 26, 2013 02:10
Decimal base exponents You can use 1 and 0 to represent true and false. I’ve seen this used in JavaScript game development in shorthand while loops. Note that if you use the negative start your array may be in reverse. You can also use while(i++<10) and you don't have to add the i++ later on inside the while.
var i=0;
while (i<9)
{
//do stuff
i++; //say
}
//shorthand
var i=9;
@nola
nola / switchshorthand.js
Created November 26, 2013 02:04
switch shorthand
switch (something) {
case 1:
doX();
break;
case 2:
doY();
break;
@nola
nola / charat.js
Created November 26, 2013 01:54
charAt() Shorthand
"myString".charAt(0);
//shorthand
"myString"[0]; // Returns 'm'
@nola
nola / foreachloop.js
Created November 26, 2013 01:53
JavaScript foreach Loop Shorthand
for (var i = 0; i < allImgs.length; i++)
//shorthand
for(var i in allImgs)
//with array
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
@nola
nola / functionArgs.js
Created November 26, 2013 01:45
Function Variable Arguments Shorthand
function myFunction( myString, myNumber, myObject, myArray, myBoolean ) {
// do something...
}
myFunction( "String", 1, [], {}, true );
//shorthand
function myFunction() {
console.log( arguments.length ); // Returns 5
for ( i = 0; i < arguments.length; i++ ) {
@nola
nola / ifshorthand.js
Created November 26, 2013 01:43
If Shorthand
if (likeJavaScript == true)
//shorthand
if (likeJavaScript)
//another version
var a;
if ( a != true ) {
// do something...
}