Skip to content

Instantly share code, notes, and snippets.

View W-Mills's full-sized avatar

Will Mills W-Mills

View GitHub Profile
@W-Mills
W-Mills / this_js_ex5.js
Last active August 20, 2019 22:04
Clairifying this in Javascript example 5
const foo = {
bar: 10,
multiplyByBar: function(...args) {
args.forEach(arg => console.log(arg * this.bar)); // arrow function does not change the context
}
};
const qux = foo.multiplyByBar.bind(foo);
qux(5); // logs 50
@W-Mills
W-Mills / this_js_ex4.js
Last active August 20, 2019 21:45
Clairifying this in Javascript example 4
const foo = {
bar: 10,
multiplyByBar: function(...args) {
args.forEach(function(arg) {
console.log(arg * this.bar);
}, this);
}
};
const qux = foo.multiplyByBar.bind(foo);
@W-Mills
W-Mills / this_js_ex3.js
Created August 20, 2019 20:59
Clairifying this in javascript example 3
const foo = {
bar: 10,
multiplyByBar: function(...args) {
args.forEach(arg => console.log(arg * this.bar));
},
};
foo.multiplyByBar(5); // logs 50
foo.multiplyByBar(5, 10, 15) // logs 50, then 100, then 150
@W-Mills
W-Mills / this_js_ex2.js
Last active August 20, 2019 20:47
Clarifying this in JavaScript example 2
const foo = {
bar: 'baz',
getBar: function() {
console.log(this.bar);
},
};
let qux = foo.getBar;
qux.call(foo); // logs 'baz'
@W-Mills
W-Mills / this_js_ex1.js
Last active August 20, 2019 20:38
Clarifying This in JavaScript example 1
const foo = {
bar: 'baz',
getBar: function() { // assigned as a property on an object, getBar is a method
console.log(this.bar); // the value of this here: foo
},
};
foo.getBar() // logs 'baz'
let qux = foo.getBar; // assigning the function getBar to the variable qux
@W-Mills
W-Mills / gist:b2ff8572aa5bee9cb791f7317d90282f
Created August 20, 2019 19:56
Clarifying this in javascript example 1
const foo = {
bar: 'baz',
getBar: function() { // assigned as a property on an object, getBar is a method
console.log(this.bar); // the value of this here is the direct-parent foo object context
},
};
foo.getBar() // logs 'baz'
const qux = foo.getBar; // assigning the function stored as the method getBar to the variable qux in the global scope
@W-Mills
W-Mills / javaScriptPedacSnippet.js
Created July 10, 2019 16:00
javaScript PEDAC Boilerplate for VSCode
{
"PEDAC": {
"scope": "javascript",
"prefix": "PEDAC",
"body": [
"/*",
"Input: $1",
"Output: $2",
"\nProblem: \n\t- $3",
@W-Mills
W-Mills / rubyPedacSnippet.js
Last active July 10, 2019 16:09
Ruby PEDAC Boilerplate Code Snippet for VSCode
{
"PEDAC": {
"scope": "ruby",
"prefix": "PEDAC",
"body": [
"=begin",
"Input: $1",
"Output: $2",
"\nProblem: \n\t- $3",
"\nClarifying Questions: \n\t- $4",
# recreation_centre_last_example.rb
module Swimmable
def swim
"Can swim here!"
end
end
class GreenSpace
attr_accessor :name, :num_trees
# recreation_centre_expanded.rb
module Swimmable
def swim
"Can swim here!"
end
end
class GreenSpace
attr_accessor :name, :num_trees