Skip to content

Instantly share code, notes, and snippets.

View kristyburge's full-sized avatar

Kristy Burge kristyburge

View GitHub Profile
@kristyburge
kristyburge / friend.js
Created July 30, 2018 23:12
Social media example using this
// Constructor Function
var Friend = function(name, password, interests, job){
this.fullName = name;
this.password = password;
this.interests = interests;
this.job = job;
};
function sayHello(){
// uncomment the console.log to see the object that 'this' points to
@kristyburge
kristyburge / new-2.js
Last active July 30, 2018 23:00
More examples using new and the object prototype
// Using the Dog prototype, create a new instance
var chester = new Dog('beagle', 'Chester', ['Gracie', 'Josey', 'Barkley']);
chester.intro(); // returns Hi, my name is Chester and I'm a beagle
console.log(chester); // returns Dog {breed: "beagle", name: "Chester", friends: Array(3), intro: ƒ}
// Here's another example:
var City = function(city, state) {
this.city = city || "Phoenix";
this.state = state || "AZ";
this.sentence = function() {
@kristyburge
kristyburge / new.js
Last active August 1, 2018 20:47
Using the new keyword
var str = new String('Hello world');
/*******
You could do the above, but it's best to avoid it (instead do like the variable str2 below)
(because JavaScript knows that anything inside single or double quotes has the type of String)
Same goes for other primitives. This is for example purposes only.
NOTE: To clarify -- the only time I ever use the new keyword in practice is when I use a function constructor and create my own object type.
*******/
@kristyburge
kristyburge / test-strict.js
Created July 30, 2018 18:06
Strict mode and This
function test() {
'use strict';
return this;
}
console.log( test() );
// returns undefined, not the Window object … interesting
@kristyburge
kristyburge / test.js
Created July 30, 2018 18:05
When 'this' is used in a normal function context
function test() {
console.log('hello world');
console.log(this);
}
test();
// hello world
// Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
@kristyburge
kristyburge / this-example.js
Created July 30, 2018 18:04
When 'this' is used inside an object, it points to the object itself
var dog = {
name: 'Chester',
breed: 'beagle',
intro: function(){
console.log(this);
}
};
dog.intro();
@kristyburge
kristyburge / beagles-chase.js
Created July 30, 2018 16:49
Using a function and assigning it to an object
var dog = {
breed: 'Beagles',
lovesToChase: 'rabbits'
};
function chase() {
console.log(this.breed + ' loves chasing ' + this.lovesToChase + '.');
// console.log(this);
}
@kristyburge
kristyburge / dog.js
Last active July 27, 2018 23:33
Create a object type with a constructor function and see 'this' in action
function Dog(breed, name, friends){
this.breed = breed;
this.name = name;
this.friends = friends;
this.intro = function() {
console.log(`Hi, my name is ${this.name} and I’m a ${this.breed}`);
return this;
};
}
@kristyburge
kristyburge / arrow-this.js
Created July 27, 2018 23:11
Arrow functions example
var objReg = {
hello: function() {
return this;
}
};
var objArrow = {
hello: () => this
};
@kristyburge
kristyburge / window.js
Created July 27, 2018 23:10
When 'this' points to the Window object
console.log(this);
// returns the Window object
// Window { postMessage: ƒ,
// blur: ƒ,
// focus: ƒ,
// close: ƒ,
// frames: Window, …}
function myFunction() {