It's a shame they decided to involve one of the worst parts of JavaScript and hardest to explain in all the examples: "this". I've written a lot of JavaScript code that doesn't use it and it's really easy to understand.
I agree that "this" may be hard to explain (especially, say if you find yourself interviewing at AMZN and some middle manager shouts at you over video to explain what "this" means) but in BlockLike it is, by design, pretty straight forward - "this" refers to the Sprite that invoked the function.
"this" is me, the BlockLike Sheep (friend of Scratch Cat).
It can be rewritten without using "this" (obviously). But can you make it easier to understand? Can you make it better at explaining the concept it does?
There is a reason JavaScript has "this" and I think this might be the reason ;)
> "this" refers to the Sprite that invoked the function
Sure, "this" is always what invoked the function. Why not just pass in the sprite to the function though and use the argument instead? That makes the learning more general for all languages and not javascript specific. IMO, there is no value in using "this".
I don't think OOP should be taught in the early stages of learning programming. Here is a simple example from the docs:
let stage = new blockLike.Stage();
let sprite = new blockLike.Sprite();
sprite.addTo(stage);
Why are you adding a stage to a sprite instead of adding a sprite to a stage? Why does sprite have a method defined on it and why is stage the parameter? I think a more consistent way to do that would be to have a function that takes both as arguments:
addSpriteToStage(sprite, stage);
Imagine that the person trying to learn programming is trying to come up with their own library to use. It wouldn't even occur to them to create free standing function. Instead they would have to waste time deciding which arbitrary class to put the method on.
> It can be rewritten without using "this" (obviously). But can you make it easier to understand?
Here is a simple change I would make for that example. I can't figure out how to share the codepen (I think creating an account might be the only way) so here's the code for the relevant part:
Unfortunately this doesn't work. I have no idea what magic that "invoke" function does, so I don't know why this doesn't work. What if you wanted the other sprite to talk when a sprite is clicked? Wouldn't you have to pass in an argument anyway. Getting a magical "this" when it happens to be the sprite that is clicked that is doing the talking seems arbitrary.
Anyway, I guess my biggest gripe is that it puts too much emphasis on OOP too early in the process of learning to program.
Are both ok. First is more intuitive cause everything else is of shape sprite.doThing
In Scratch the IDE does the creation/association. So the design choice here is - should a Sprite be “magically” added to Stage upon construction. I chose not to because removing a Sprite is also a thing and symmetry is important.
But those are semantics - the whole thing is indeed OOP because... Scratch is OOP.
Sprites on a stage interact.
BlockLike didn’t invent any of it, it just attempts to creat a syntax that is as similar as possible to Scratch, thus allowing kids to use the same concepts and patterns they already know as a basis upon which to acquire new ones.
So while passing sprites around is valid javascript and valid in BlockLike (your example doesn’t work only because to expect your function do do something javascript can’t do out of the box - you expect it to wait) this is not really the way to go here. The way to go is simple, the sprite that invokes is “this” and if you want a click on one sprite to have an effect on another sprite, let another sprite invoke. (For playful fun the example has been updated: https://codepen.io/BlockLike/pen/JpjVOp?editors=1010)
But, here’s the interesting thing that “emerged out of the design” and that I really like - one learns that one can pass around functions.
.say() gets a string, .wait() gets a number. whenClicked() gets a function (so does invoke()). Those are methods of the Sprite. What else is a method that gets a function? How about forEach()? and from there it’s off to the races... ;)
> the whole thing is indeed OOP because... Scratch is OOP.
Right. I guess that's the core of my problem. I don't think you should be teaching beginning programmers OOP. It pollutes their minds too early and they don't appreciate the separation of data and code. And yes, I am biased because I dislike OOP.