Repository navigation
Wilfred Morgan: Javascript III Project #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
734d967
f8f7d72
71887e0
ab4abb0
4551973
73e8c17
a53e294
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,15 @@ | ||
| # dependencies | ||
| /node_modules | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,68 @@ | ||
| /* The for principles of "this"; | ||
| * in your own words. explain the four principle for the "this" keyword below. | ||
| * | ||
| * 1. | ||
| * 2. | ||
| * 3. | ||
| * 4. | ||
| * 1. Window (Default) Binding: this binds to the global object. It is the default setting. | ||
| * 2. Implicit Binding: occurs when dot notation is used to invoke a function. | ||
| Whatever is to the left of the dot becomes the context for ```this``` in the function. | ||
| * 3. Explicit Binding: occurs when .call(), .apply(), or .bind() are used on a function. | ||
| * 4. New Binding: occurs when building new objects or when explicitly binding an existing function to the object | ||
| * | ||
| * write out a code example of each explanation above | ||
| */ | ||
|
|
||
| // Principle 1 | ||
|
|
||
| // code example for Window Binding | ||
| function myWinBindFunc () { | ||
| console.log(this) | ||
| } | ||
| myWinBindFunc() | ||
|
|
||
| // Principle 2 | ||
| function myWinBindFuncStrict () { | ||
| 'use strict' | ||
| console.log(this) | ||
| } | ||
| myWinBindFuncStrict() //undefined | ||
|
|
||
| // Principle 2 | ||
| // code example for Implicit Binding | ||
| let animal = { | ||
| species: 'dog', | ||
| name: 'Snoopy', | ||
| identify: function () { | ||
| console.log(`${this.name} is a ${this.species}`) | ||
| } | ||
| } | ||
| animal.identify() | ||
|
|
||
| // Principle 3 | ||
|
|
||
| // code example for New Binding | ||
| // Use a 'constructor' which is a function that creates an object | ||
| function Person(name) { | ||
| this.name = name, | ||
| this.greeting = function () { | ||
| return `Hello, my name is ${this.name}` | ||
| } | ||
| } | ||
|
|
||
| let wilfred = new Person('Wilfred') | ||
| console.log(wilfred) | ||
| console.log(wilfred.greeting()) | ||
|
|
||
| // Principle 4 | ||
| // code example for Explicit Binding | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably the best example of explicit binding I've seen. It's difficult for me to come up with a good reason to use it.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Joe! To answer your comment:
This particular code allows you to combine an object and an array and dynamically iterate through the array (instead of listing item1, item2, item3, etc). An example I could think of for using this code in real life for creating a mail merge of customer information into a marketing form letter. So if I take my code from Exercise bloominstituteoftechnology#4 and extend it for this example we've got something like this.... Being able to do things like that will make you a hero with the marketing folks and management. Haha! There's more you can do with this code but this shows the possible value of using explicit binding. |
||
| //Creating an object from the Person constructor in Principle #3 | ||
| let george = new Person('George') | ||
| console.log(george) | ||
| console.log(george.greeting()) | ||
|
|
||
| //Create an array for demonstration purposes | ||
| const hobbies = ['painting', 'scuba diving', 'golf', 'reading', 'cooking'] | ||
|
|
||
| //Create a function that will use data from the newly created object and array | ||
| function introduce (list) { | ||
| return `${this.greeting()}, my hobbies are ${list.map((item, index) => index !== list.length-1 ?' '+ item: ' and ' + item)}.` | ||
| } | ||
| // Explicity bind the object and array to the introduce function using the .call() method | ||
| console.log(introduce.call(george, hobbies)) | ||
|
|
||
| // code example for Explicit Binding | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell if this is an Easter Egg for me or a GI Joe reference! This is probably the best game I've yet seen for this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is a GI Joe, reference. I purposely mixed genres to see if anyone would notice. Haha!