Skip to content

Instantly share code, notes, and snippets.

@fragje
Created April 3, 2012 19:25
Show Gist options
  • Select an option

  • Save fragje/2294906 to your computer and use it in GitHub Desktop.

Select an option

Save fragje/2294906 to your computer and use it in GitHub Desktop.
JavaScript: reference
// JavaScript reference
// Operators
// Variables
/**
* Conditionals
*/
/**
* Arrays
*/
var myArray = [10, 30, 23];
console.log(myArray[1]); //30
/**
* Functions
*/
var square = function(length) {
return length * length;
};
/**
* Objects
*/
// Literal notation
var frank = {
name: "Frank",
age: 29
};
// Constructor notation
var frank = new Object();
frank.name = "Frank";
frank.age = 29;
// Access properties in objects...
// ...with dot notation
console.log(frank.name);
// ...with bracket notation
console.log(frank["name"]);
// Methods
// ...is function inside a object or constructor
frank.setAge = function (newAge) {
frank.age = newAge;
}
// Custom constructor
var Person (name, lastName) {
this.name = name;
this.lastName = lastName;
this.fullName = function() {
console.log(this.name + " " + this.lastName);
};
}
var ola = new Person("Ola", "Nordmann"); //Makes a new object with the Person constructor
ola.fullName(); //Use "()" since it is a function/method
// Make a object with the custom constructor
var frank = new Person ("Frank", 29);
// Typeof
// Check what type of variable it is.
var myString = function(x, y) {
return x + y;
}
console.log(typeof myString); // would print "function"
// Has own property
// Check if the property of an object exists. Returns true or false.
console.log(frank.hasOwnProperty("name");
// For in loop
// Check properties in a object
for(var x in frank) {
console.log(x); // Prints property label
console.log(frank[x]) // Prints property value
}
// OOP
// Prototype
// Add a default method to a constructor
className.prototype.newMethod = function() {
statements;
};
// Private and public properties
function Person(first, last) {
this.first = first; //Public properies
this.last = last;
var money = 3100; //Private propery
var loan = function() { //Private method
//code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment