From aef317e52b771d26969d5bdbb50ff8c57255f9c8 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 18 May 2018 11:15:05 -0700 Subject: [PATCH] Update README.md --- 9. ES6/README.md | 60 ++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/9. ES6/README.md b/9. ES6/README.md index b3df80d..c25de4c 100644 --- a/9. ES6/README.md +++ b/9. ES6/README.md @@ -283,54 +283,34 @@ console.log('ES6 square:',squareES6(6)); ### Binding -Here is an example of the arrow function binding this +with callbacks, this can get redefined: ```JavaScript -// Create constructor function, inputs name, has default friend values -function Person ( name , friends = ["Charlie", "Dennis", "Ron", "Sweet Dee", "Frank"]){ +function Person(name){ this.name = name; - this.friends = friends; - - //Add four methods: - - // The first, `secret Admirer`, `this.name` is undefined in the forEach function - this.secretAdmirer = function (){ - this.friends.forEach(function ( f ){ - console.log( this.name + " sends flowers to " + f ); - }); - } - - //The second one is the way we got around the issue of `this` - which was to set the desired `this` to a variable called `that` or `self` or something similar: - - this.giveOldSchoolLove = function (){ - var self = this; - this.friends.forEach(function ( f ){ - console.log( self.name + " likes " + f ); - }); - } - - // we could also use .bind() - this.giveBindingAffection = function (){ - this.friends.forEach(function ( f ){ - console.log( this.name + " makes friendship bracelets for " + f ) - }.bind(this)); + this.logName = function(){ + setTimeout(function(){ + console.log(this.name); + },500) } +} +var me = new Person('Matt'); +me.logName(); +``` - //Finally, by using the arrow function, `this` is already bound: +arrow functions fix this: - this.giveNewSchoolLove = function (){ - this.friends.forEach(f => console.log( this.name + " hearts " + f )); +```javascript +function Person(name){ + this.name = name; + this.logName = function(){ + setTimeout(()=>{ + console.log(this.name); + },500) } } - -//See examples -k = new Person ( "Matt" ); -console.log( 'Secret Admirer:' ); -k.secretAdmirer(); -console.log( 'Show old school love:' ); -k.giveOldSchoolLove(); -console.log( 'Show new school love:' ); -k.giveNewSchoolLove(); +var me = new Person('Matt'); +me.logName(); ``` ## Support for Classes