Skip to content

Instantly share code, notes, and snippets.

@bvancea
Created April 8, 2015 19:19
Show Gist options
  • Save bvancea/d132f96dd59678bfa317 to your computer and use it in GitHub Desktop.
Save bvancea/d132f96dd59678bfa317 to your computer and use it in GitHub Desktop.
JavaScript basic ideas
//creating an object and adding methods to it
var rabbit = {};
rabbit.speak = function(line) {
console.log("The rabbit says '" + line + "'");
}
rabbit.speak("I'm alive");
//can also add previously defined functions to the objects
function speak(line) {
console.log("The " + this.type + " rabbit says '" + line + "'");
}
speak("Hi, I seem to be undefined");
var whiteRabbit = { type: "white", speak: speak };
var fatRabbit = { type : "fat", speak: speak};
whiteRabbit.speak("Oh my ears and whiskers, how late it's getting!");
fatRabbit.speak("I could use a carrot right now.");
//can call functions using apply and call on random objects
speak.apply(fatRabbit, ["Burp"]);
speak.call({type : "old"}, "Oh my.");
//prototype magic
var empty = {};
console.log(empty.toString);
console.log(empty.toString());
console.log(Object.getPrototypeOf({}) == Object.prototype);
console.log("Prototype of Object.prototype: " + Object.getPrototypeOf(Object.prototype));
//using the prototype for creating objects
var protoRabbit = {
speak: function(line) {
console.log("The " + this.type + " rabbit says '" + line + "'");
}
};
var killerRabbit = Object.create(protoRabbit);
killerRabbit.type = "killer";
killerRabbit.speak("SKREEE!");
//constructors - any functions called with new is treated as a constructor
function Rabbit(type) {
this.type = type;
}
var killerRabbit = new Rabbit("killer");
var blackRabbit = new Rabbit("black");
console.log(blackRabbit.type);
//constructors get an empty prototype that is derived from Object.constructor
//this prototype is held on the "prototype" property and is not the actual
//prototype of the Rabbit function ( which is actually Function.prototype)
Rabbit.prototype.speak = function(line) {
console.log("The " + this.type + " rabbit says '" + line + "'");
}
blackRabbit.speak("Doom...");
Rabbit.prototype.teeth = "small";
killerRabbit.teeth = "long, sharp, and bloody";
console.log(killerRabbit.teeth);
//prototype inheritance
Rabbit.prototype.dance = function() {
console.log("The " + this.type + " rabbit dances a jig.");
}
killerRabbit.dance();
//how prototypes can cause problems
var map = {};
function storePhi(event, phi) {
map[event] = phi;
}
storePhi("pizza", 0.069);
storePhi("touched tree", -0.081);
// add a property to the Object prototype
Object.prototype.nonsense = "hi there"
for (var name in map) {
console.log(name);
}
Object.defineProperty(Object.prototype, "hiddenNonsense",
{enumerable : false, value: "hi"});
for (var name in map) {
console.log(name);
}
console.log("toString" in map);
console.log(map.hasOwnProperty("toString"));
//prototype-less objects - usefull as someone could actually update
//the hasOwnProperty function in the prototype - such a drag
var map = Object.create(null);
map["pizza"] = 0.069;
console.log("toString" in map);
console.log("pizza" in map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment