-
Notifications
You must be signed in to change notification settings - Fork 0
Sachin benny #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?
Sachin benny #1
Changes from all commits
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 |
|---|---|---|
|
|
@@ -14,4 +14,4 @@ | |
| <body> | ||
| <h1>JS III - Check your work in the console!</h1> | ||
| </body> | ||
| </html> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,26 +1,69 @@ | ||||||
| /* The for principles of "this"; | ||||||
| * in your own words. explain the four principle for the "this" keyword below. | ||||||
| * | ||||||
| * 1. | ||||||
| * 2. | ||||||
| * 3. | ||||||
| * 4. | ||||||
| * 1. Global - this will call the window of the browser because the binding of this is Global | ||||||
|
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. Generally, it is the "global object". Whatever that is depends on the environment. On browsers, it indeed is |
||||||
| * 2. Implicit - this will call the object to the left of the "." where the function is called | ||||||
|
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. Slightly better wording is that |
||||||
| * 3. New - its used for constructors. This will call the object being created by the constructor when it is called | ||||||
|
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. Terminology is probably "bind" for |
||||||
| * 4. Explicit - Its used to specify to this what exactly to call | ||||||
| * | ||||||
| * write out a code example of each explanation above | ||||||
| */ | ||||||
|
|
||||||
| // Principle 1 | ||||||
| function globalisation(){ | ||||||
| console.log(this) | ||||||
| } | ||||||
| globalisation(); | ||||||
|
|
||||||
| // code example for Window Binding | ||||||
|
|
||||||
| // Principle 2 | ||||||
|
|
||||||
| // code example for Implicit Binding | ||||||
| const obj1 = { | ||||||
| name:"Sachin" , | ||||||
| englishpremier: function() | ||||||
| { | ||||||
| console.log (`${this.name} love the english premier league`) | ||||||
| } | ||||||
| } | ||||||
| obj1.englishpremier() | ||||||
|
|
||||||
| // Principle 3 | ||||||
|
|
||||||
| // code example for New Binding | ||||||
| function CreateHuman(humanone){ | ||||||
| this.name = humanone.name | ||||||
| this.gender = humanone.gender | ||||||
| this.garden = humanone.garden | ||||||
| } | ||||||
| CreateHuman.prototype.hello = function() { | ||||||
| return(`${this.name} from ${this.garden} says hello`) | ||||||
| } | ||||||
|
|
||||||
| const human = new CreateHuman( | ||||||
| {name: 'Adam', | ||||||
| gender: 'M', | ||||||
| garden: 'Eden'} | ||||||
| ) | ||||||
| console.log(human.hello()) | ||||||
| // Principle 4 | ||||||
|
|
||||||
| // code example for Explicit Binding | ||||||
| // code example for Explicit Binding | ||||||
| function Inventhuman(attributes){ | ||||||
| this.namej = attributes.namej | ||||||
| this.genderj = attributes.genderj | ||||||
| this.gardenj = attributes.gardenj | ||||||
| } | ||||||
|
|
||||||
| function Married(name1, name2) | ||||||
| { | ||||||
| Inventhuman.apply(this,name1) | ||||||
|
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. If you use
Suggested change
Alternatively, Inventhuman.call(this, name1); |
||||||
| this.name2 = name2 | ||||||
| } | ||||||
| Married.prototype = Object.create(Inventhuman.prototype) | ||||||
| Married.prototype.whosmarried = function() | ||||||
| {`${this.name} is from ${this.garden} and is married to ${this.name2}`} | ||||||
|
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. Remember, if you want an output from a function/method, what keyword must be present in the body of the function? Right now, we're getting a big fat undefined from the console log. Anything in this line cluing you in to why? |
||||||
|
|
||||||
| const marriage = new Married ({namej:'Adam',genderj:'M', gardenj:'Eden'}, 'Eve') | ||||||
| console.log(marriage.whosmarried()) | ||||||
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.
Well done setting up the constructor functions and prototypes! 💯