Skip to content

Instantly share code, notes, and snippets.

@pricees
Last active February 24, 2016 15:49
Show Gist options
  • Select an option

  • Save pricees/7bd0ae83bc92258d4dc7 to your computer and use it in GitHub Desktop.

Select an option

Save pricees/7bd0ae83bc92258d4dc7 to your computer and use it in GitHub Desktop.
Javascript newbie review
///////////////////////////////
///////////////////////////////
// "this"
///////////////////////////////
///////////////////////////////
// Global object
// - Node: reference via "global"
// - Browser: reference via "window"
//
global.word = 'Hello';
/////////////////////////////////
// Context 1: In unbound function
/////////////////////////////////
function foo() { return this.word; }
console.log("foo()'s this has global bind: " + foo());
/////////////////////////////////
// Context 2: Explicit binding
/////////////////////////////////
var obj = { word: 'Good bye'}
console.dir(obj);
console.log("foo() expliciting binding this to obj: " + foo.apply(obj));
/////////////////////////////////
// Context 3: Binding function to object
/////////////////////////////////
obj.bar = foo;
console.log("obj.bar === foo: " + obj.bar === foo);
console.log(obj.bar());
console.log(obj.bar.apply(this));
obj.yetAnotherFunction = function() { return this.word; };
console.log(obj.yetAnotherFunction());
var wowAnotherFunction = function() { return this.word; };
wowAnotherFunction();
wowAnotherFunction.apply(obj);
/////////////////////////////////
// Context 4: Function as constructor...or something
/////////////////////////////////
function Foo() { this.word = 'Hi;' }
Foo()
console.log(Foo)
console.log(this.word);
var myNewObject = new Foo()
console.log(foo.apply(myNewObject));
console.log(myNewObject);
///////////////////////////////
///////////////////////////////
// JS Inheritance
///////////////////////////////
///////////////////////////////
var Car = function(color, sound) {
this.color = color;
this.sound = sound;
};
Car.prototype.wheels = 4;
Car.prototype.honk = function() {
console.log("This car goes " + this.sound);
};
var honda = new Car("black", "meep");
honda.honk();
var kea = new Car("blue", "beep");
kea.honk()
console.log(kea.wheels)
var Plane = function() {
this.wheels = 100
}
Plane.prototype = new Car("blue", "meep")
var p = new Plane()
p.sound = "vrooom"
console.log(p.wheels)
console.log(p.sound)
console.log(p.color)
p.honk();
console.log(
"\n///////////////////////////////" +
"\n///////////////////////////////" +
"\n// bind, apply, call" +
"\n///////////////////////////////" +
"\n///////////////////////////////");
global.something = "Accessing global"
var baz = function() { console.log(this.something) }
baz()
global.something = "Accessing global"
var obj = { something: "Accessing something awesome" }
console.log("apply vs call?");
console.log("apply take a *this* arg, and an array of secondary arguments")
console.log("call take a *this* arg, and a list of arguments")
baz.apply(obj /*, [foo, bar baz] */);
baz.call(obj /*, foo, bar baz */);
console.log("bind returns a method with its *this* binding bound to the first arg")
// bind applys this.
baz_be_bound = baz.bind(obj)
baz_be_bound();
baz();
////////////////////////////////////
// MORE
////////////////////////////////////
var Car = function() { this.type = "car" };
var Mazda = function() { this.type = "mazda" };
var AnotherCar = { type: "another car" };
Mazda.prototype = new Car();
var c = new Car();
var m = new Mazda();
var a = Object.create(AnotherCar);
console.log(c instanceof AnotherCar); // false
console.log(c instanceof Car); // true
console.log(c instanceof Mazda); // false
console.log(Object.getPrototypeOf(m)); // { type: "car" } It's an object!
console.log(m instanceof Mazda); // true
console.log(m instanceof Car); // true
console.log(Object.getPrototypeOf(a)); // { type: "another car" } It's an object!
console.log(a instanceof Mazda); // false
console.log(a instanceof Car); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment