Skip to content

Instantly share code, notes, and snippets.

@DawnPaladin
Created August 15, 2016 17:44
Show Gist options
  • Save DawnPaladin/6694a631b8c0d063cdf38882dbb1be35 to your computer and use it in GitHub Desktop.
Save DawnPaladin/6694a631b8c0d063cdf38882dbb1be35 to your computer and use it in GitHub Desktop.
JavaScript reference guide
OBJECTS
=====
// There are two ways you can create objects: Literal notation and construction notation.
//
// Literal notation uses curly braces:
var james = {
job: "programmer",
married: false,
greet: function() {
console.log("Hello!");
}
};
// Constructor notation creates a constructor that you can use to churn out lots of similar objects, effectively turning it into a class. Functions in JavaScript are always objects, which means they have their own domain, can have their own properties, children, etc.
function Person(job, married) {
this.job = job;
this.married = married;
}
// Then you create an object using the "new" keyword:
var gabby = new Person("student", true);
// A method is a function that's built into an object:
function Person(job, married) {
this.speak = function() {
console.log("Hello!");
}
}
// Or, using literal notation:
var james = {
speak: function(speech) {
console.log("Hello, I am feeling " + speech);
}
};
// OBJECT PROPERTIES
// Normally you access object properties using dot notation (someObj.propName). But sometimes it's useful to treat the property name as a variable. You can do that by accessing the object as if it were an array.
var someObj = {propName: someValue} //object created
var myProperty = "propName";
someObj[myProperty] //These two lines
someObj["propName"] //do exactly the same thing.
// This comes into play when using "for-in" loops:
var nyc = {
fullName: "New York City",
mayor: "Michael Bloomberg",
population: 8000000,
boroughs: 5
};
for(var i in nyc) {
console.log(i); // Prints the name of each of nyc's properties
console.log(nyc[i]); // Prints the value of each of nyc's properties
}
// You can check whether a property exists with hasOwnProperty
console.log(nyc.hasOwnProperty('mayor')) // prints "true", because nyc has a mayor
// You can check what type a property is (boolean, string, number, function, etc.) with typeof
console.log(typeof nyc[mayor]) // prints "string"
CLASSES
=====
// Classes are just objects with children.
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs
}
// creates a class named Animal with two properties (name and numLegs) which can be passed in as parameters.
// Generally you'll want to define the class as much as you can up-front, but if you need to modify it afterwards, you can do so by making changes to the prototype:
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// adds a method to the Animal class called sayName.
// This is also how you set up inheritance.
Penguin.prototype = new Animal();
// changes Penguin into a subclass of Animal.
// To make an object that's a member of a class:
var george = new Penguin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment