Skip to content

Instantly share code, notes, and snippets.

@kristyburge
Created July 30, 2018 23:12
Show Gist options
  • Save kristyburge/229af0d3c87cc879e90e82a86b4fecdb to your computer and use it in GitHub Desktop.
Save kristyburge/229af0d3c87cc879e90e82a86b4fecdb to your computer and use it in GitHub Desktop.
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
// console.log(this);
return `Hi, my name is ${this.fullName} and I'm a ${this.job}. Let's be friends!`;
}
// Create one or multiple instances of the Friend prototype with the keyword 'new'
var john = new Friend('John Smith', 'badpassword', ['hiking', 'biking', 'skiing'], 'teacher');
console.log(john);
// Assign the function to the greeting key on the john object
john.greeting = sayHello;
// Call the the new method
console.log( john.greeting() );
// Remember, you can't call sayHello() as a function; it will return "Hi, my name is undefined and I'm a undefined. Let's be friends!"
// Because the function's context is global and the window object does NOT have the keys that belong to the Friend prototype
console.log( sayHello() ) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment