Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. "this" is the pointer to the window/console object
* 2. "this" is the pointer to the function preceding the dot, where this is called
* 3. " this" is the pointer to the specific instance of the object created and returned by the constructor function
* 4. "this" is a pointer to a explicit context using 'call' and 'apply'
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,64 @@

// code example for Window Binding

function writeName(name){
return this;
}

console.log(writeName('Ryan'))





// Principle 2

// Implicit binding:

// code example for Implicit Binding
'use strict'
const Person = {
name:'Ryan',
location:'New York',
speak: function(country){
return `Hi, I am ${this.name}. I am from ${country} `
}
};

console.log(Person.speak("USA"));

// Principle 3

// code example for New Binding

function Person(name,location){
this.name = name,
this.location = location,
this.speak = function(country){
return `My name is ${this.name}. I am from ${country}`
};
}

const ryan = new Person('Ryan','NYC');
const sam = new Person('Sam','Boston');
console.log(ryan.speak('USA'));

console.log(ryan.speak.call('Denver'));

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

function Person(name,location){
this.name = name,
this.location = location,
this.speak = function(country){
return `My name is ${this.name}. I am from ${country}`
};
}

const ryan = new Person('Ryan','NYC');
const sam = new Person('Sam','Boston');

console.log(ryan.speak('USA'));
console.log(ryan.speak.call(sam,'USA'));