Chapters
33 Chapters
|
3:02:08
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.2.0
Subscribe to download the code!Compatible PHP versions: ^7.2.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
11.
Fixing "this" with bind()
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial uses an older version of Symfony... but since it's a JavaScript tutorial, the concepts are still ? valid!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.2.0",
"symfony/symfony": "3.1.*", // v3.1.10
"twig/twig": "2.10.*", // v2.10.0
"doctrine/orm": "^2.5", // v2.7.1
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
"symfony/swiftmailer-bundle": "^2.3", // v2.4.0
"symfony/monolog-bundle": "^2.8", // 2.12.0
"symfony/polyfill-apcu": "^1.0", // v1.2.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"friendsofsymfony/user-bundle": "~2.0@dev", // dev-master
"doctrine/doctrine-fixtures-bundle": "~2.3", // v2.4.1
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"friendsofsymfony/jsrouting-bundle": "^1.6" // 1.6.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.1
"symfony/phpunit-bridge": "^3.0" // v3.1.6
}
}
25 Comments
Wow, how weird is JS, anyway, as an excirsise I thought I would do the more involved version of the anonymous function to
onRowDelete, I finally got it to work and I can see why you chose the other solution.I ended up having to do the
self=thistrick to make another anonymous function work correct, it would seem even if you use bind to make JS behave, you still end up having to keep your eye out for anonymous functions and be very aware of thethisobject and what it's pointing too.This was my result, did I miss something?
Hey Peter!
Ha! Yea, weird stuff, right! Fortunately, if you keep going to the next tutorial about ES6 and the arrow function, we'll be able to work around all of this in a cleaner way (but because you got through this stuff, you'll really understand how this "this" business works).
So let's check out your solution. I love the
this.onRowDelete.bind(this, $row)- that IS the correct way to do this by using bind. You also could have done something crazy like this:Bananas, right!? You're creating an anonymous function, and so "this" should not be "this" inside of it. But, we bind that anonymous function to this... which allows us to use "this" inside :p.
Anyways, about the
let self = thispart in onRowDelete(), if you wanted to avoid that, you would do a similar trick:I hope that makes sense (and that I didn't screw anything up 😉)
Cheers!
Thanks Ryan
That really helps me understand.. mind you I just finished the ES6 course and had to have a moment with ES6 in a dark room for some alone time and some klennex..
I love the other two solutions too, its all about binding it to the function to make sure this is availble as what its been set to before the anonymous function! Thank you for teaching me all that.
Once I finished that it made everything simpler to implement, but knowing how this works is super very important in my opinion.
Hi Ryan
Earlier on you mentioned that we are not adding the () to the handlers within in initialize function. Now we are calling the bind function so was "this" the reason earlier on of not calling it directly?
Hey Graymath technology!
Not quite - but I think this is a good question :). Forget about the "bind" thing for a moment. So, we have some code like this:
You need to think of
this.handleRepLogDeleteas a variable that points to a function. We're basically saying:We don't do this in PHP often, but it's possible. It would be like this:
So, regardless of the "bind" stuff, what we want to do in JavaScript is pass a reference of a function to jQuery - not actually call that function when we are telling jQuery that we want to listen to some on "click" event. When we add bind later, it's really an unrelated change: we still want to pass a reference to a function, not call that function. When you write
this.handleRepLogDelete.bind(this), the bind() function creates a new function and returns a reference to that function. So, we're still passing a reference to a function to jQuery, just a slightly different function that guarantees that "this" will be what we expect :).I hope that helps! I have problems with this sometimes because PhpStorm likes to auto-complete the ()
Cheers!
Hey Ryan,
I realize that arrow function didn't exist when this video came out, but seeing all those grossed out people in the comments makes me feel like an appendix to it could be appropriate.
Thanks!
Hey there!
It wasn't that the arrow function didn't exist... but rather that we chose to show that in the *next* tutorial - all about the ES6 features: https://knpuniversity.com/s.... Of course, these days, the arrow function is becoming so universal, it should indeed be shown earlier and earlier :). Oh, but bind() does still have a place - even in our new React tutorial, we often use bind to guarantee that callback methods will *always* be bound to this, regardless of how their called. But, the arrow function definitely removes a lot of ugliness.
Cheers!
Hey Ryan,
thanks for your reply!
I understand that you wanted to keep ES6 things separate. On the other hand, when beginning to learn JavaScript, it must feel like an insane language due to this "this" thing (among other weird things). I hope that doesn't put off beginners from exploring more! :)
Hey @asd
Indeed JS is a weird programming language, but after you unveil its mysteries, it's actually fun to work with and especially if you are working with ReactJS
Cheers!
This just shows me how bad a language javascript is.
Hey Thao L.
I would preffer to say how *weird* javascript is, but, after you know how it works internally you end-up loving it ;)
Cheers!
Hi Guys:
Can we just have
`var self = this
`as RepLogApp variable. so that you don't have to bind each internal function. so you don't need to have this link
`var $link = $(e.currentTarget);
`.
when you call an RepLogApp function, you just need to do the following
`self.handleRepLogDelete
`. Shouldn't this would be easier?
`var newThis = {cat: 'meow', dog: 'woof'}; var boundWhatIsThis = this.whatIsThis.bind(this);
`from the code above, it confused me because when I explicitly call() in JavaScript, I would expect this would be 'newThis'. Call() in JavaScript has a meaning of changing context to 'this'
Hey Jian,
Do whatever you like most. I think it's easier for you because you just get used to it :) But bind() feature allow you to get rid of "var self = this", and actually, the function behave itself as PHP functions where $this refers to the current object, not to something that calls this function.
Cheers!
Hey there would you please share your PHPStorm color schema with us? and maybe some other options too
Hey Mesut,
We use Darcula PhpStorm theme in our recent screencast, including this one. Check this free tutorial about PhpStorm to know more interesting features in PhpStorm we love and use from screencast to screencast:
https://knpuniversity.com/s...
Cheers!
Hey Ryan!
Are your suggestions concerning *this* actually js best-practices? Or would you rather see them as 'a great way to deal with js for php geeks'?
Hi Max!
If you are referring to use .bind() to guarantee that "this" is this, then absolutely. I was just digging around in the core of a really complex JS library this morning (webpack) and you could see it being used there :).
If you're referring to using
var self = this;- this is not (anymore) considered a great practice... but you need to wait until the next tutorial about ES6 to have a better solution. With the tools we have up to now, it's the only way to solve it!And if you're referring to something entirely... let me know ;).
Cheers!
Hey Ryan! Cool! I was simply wondering as you are trying to refer to PHP al the time (which is great!)...
Best
Max
Using .bind() just made JS even more weird for me, tbh
boykodev
I got you, but that's a detail about callbacks, sometimes you need to control the reference to "this", or you would end up with an unwanted behaviour. In short, JS is WEIRD in general
Have a nice day
Yeah, that's what I've figured. You too!
Hello!
var self = this;
didn't work, getting error:
TypeError: self.updateTotalWeightLifted is not a function
Debugging shows "this" is a clicked delete link, but not the desired RepLogApp object.
I work in phpstorm, set to ES6 and then to ES5 yet same result.
What I miss?
Thanks
==========
I found, forgot to bind(this)
this.handleRepLogDelete.bind(this)
in initialize function.
Thanks
Haha, nice, you figured out what was the problem. Javascript is kind of a weirdo :p
what about using arrow functions to not loose focus of this? look at the following example
Hey Maik,
Yeah, good point! We love arrow functions, really - they're our favorite! And you can write them fast - fewer code to type :p But we just do not explain them yet in this course if I'm correct, so we just didn't want to complicate things here. But yeah, if you know the difference and see you can use the arrow function instead - use it! Well, sometimes regular functions might have some valid use cases when better use *them*, but probably most of the time you can use arrow functions instead.
Cheers!
"Houston: no signs of life"
Start the conversation!