Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Its all about closures. Different complexFunctions has its own calls variable becouse of closure enviroment.
Being new to javascript I didn't understand how this worked for different complexFunctions. Looking into scoping of variables led me to javascript closures and it all makes sense now.
Tried to pull this off with a simple comparision:
the problem with this approach was that cached functions could return any value (including undefined value), so there's no way to discern yet uncached function calls from cached function calls that returned undefined. So far I thought that
is semantically equivalent to simple comparision mentioned above. I was wrong because this approach can handle the job and discern unset undefined from set undefined. Thanks for casting some light on this subtlety.
Here's some sample code to demonstrate...
Now we test it...
If we call it as a function,
this
is the global object and it can access global variables as properties of it:And now since it's
this
was the global object,myvar
has leaked outside of the function:If it's called as a constructor (like it's supposed to be), its
this
is theMyObj
object you've constructed, with its ownmyvar
property (and now nothing leaks into the global, so the globalmyvar
would still be"foo"
afterward):Now, since we've created a new
MyObj
object namedmyobj
, what about themethod1
function?Ordinarily, if you call
myobj.method1
, itsthis
value will bemyobj
:If you create an alias of the method, it becomes orphaned from its parent object and it doesn't work anymore (and its
this
is the global object, sothis.myobj
is the global that had been defined above):Now, if you bind the method to itself, its
this
is itself, and it doesn't work; also, sincemethod1
doesn't have any property namedmyvar
,this.mybar
will be undefined:Last, the correct way, by binding it to the
myobj
object, which results in the same thing as callingmyobj.method1()
does:First of all, if
func
doesn't use thethis
value, then it doesn't matter what the first argument is. It can be anything because it's never used.Secondly, inside of a function,
this
is never ordinarily the function itself. It's one of two things:this
objectnew
So you generally never need to call
func.bind(func)
, becausefunc
would not normally expect itself to be its ownthis
object. And when you need to bind an object of a class to one of its methods, you writeobj.method.bind(obj)
so that the method is bound to the object. You wouldn't writeobj.method.bind(obj.method)
.First argument in call, according to documenation is "this" for the function. That allows you to write complex constructors.
Why do you need to pass null as first argument to apply? I thought you'll need to pass func as first argument.
Thanks,
Indeed. I have a solution which generates a valid pass1, I believe a valid pass2, and a proper pass3, but I can't submit because this first test-case is too strict.
Test driven development at its finest.