At first I thought this solution only works because of missing test cases, because it doesn't account for 'eggs' that are not after consonants, like word = 'egg'. This would be impossible by definition, though, since that would have to be mutated to 'egegggegg'
Agree, please fix the problem. Using python.
Verifying that fib(-91) == 4660046610375530309
-4660046610375530309 should equal 4660046610375530309
How come negative fibonacci numbers equal positive numbers?
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.
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
!(key in calls)
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.
functionMyObj(myvar) {
this.myvar=myvar;
console.log("this.myvar is "+this.myvar);
if (thisinstanceofMyObj) {
console.log("Called as constructor: new MyObj(...)");
} else {
console.log("Called as an ordinary function: MyObj(...)");
}
returnthis;
}
MyObj.prototype.method1=function () {
console.log("this.myvar is "+this.myvar);
if (thisinstanceofMyObj) {
console.log("method1 was called on a MyObj object");
} else {
console.log("Error: method1 must be called on a MyObj object");
}
};
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:
MyObj("foo"); // call as function// "this.myvar is foo"// "Called as an ordinary function: MyObj(...)"
And now since it's this was the global object, myvar has leaked outside of the function:
console.log(myvar);
// "foo"
If it's called as a constructor (like it's supposed to be), its this is the MyObj object you've constructed, with its own myvar property (and now nothing leaks into the global, so the global myvar would still be "foo" afterward):
varmyobj=newMyObj("bar"); // call as constructor// "this.myvar is bar"// "Called as constructor: new MyObj(...)"
Now, since we've created a new MyObj object named myobj, what about the method1 function?
Ordinarily, if you call myobj.method1, its this value will be myobj:
myobj.method1();
// "this.myvar is bar"// "method1 was called on a MyObj object"
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, so this.myobj is the global that had been defined above):
varfn1=myobj.method1; // fn1 is orphaned from its myobj object; its this is the global objectfn1(); // it can access the global because it's not bound to anything// "this.myvar is foo"// "Error: method1 must be called on a MyObj object"
Now, if you bind the method to itself, its this is itself, and it doesn't work; also, since method1 doesn't have any property named myvar, this.mybar will be undefined:
varfn2=myobj.method1.bind(myobj.method1); // now it's bound to itselffn2(); // it can't access the global now; it's bound to itself// "this.myvar is undefined"// "Error: method1 must be called on a MyObj object"
Last, the correct way, by binding it to the myobj object, which results in the same thing as calling myobj.method1() does:
varfn3=myobj.method1.bind(myobj); // it's bound to its myobj, so it's not an orphanfn3();
// "this.myvar is bar"// "method1 was called on a MyObj object"
First of all, if func doesn't use the this 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:
the global this object
an object you've created using new
So you generally never need to call func.bind(func), because func would not normally expect itself to be its own this object. And when you need to bind an object of a class to one of its methods, you write obj.method.bind(obj) so that the method is bound to the object. You wouldn't write obj.method.bind(obj.method).
This comment is hidden because it contains spoiler information about the solution
I had the same thing :) only thing is, it doesn't make sense to import an entire module when you only need one method
or not source.data
this line would cause an exception to be thrown in the valid scenario where source.data is zeroAt first I thought this solution only works because of missing test cases, because it doesn't account for 'eggs' that are not after consonants, like word = 'egg'. This would be impossible by definition, though, since that would have to be mutated to 'egegggegg'
Agree, please fix the problem. Using python.
Verifying that fib(-91) == 4660046610375530309
-4660046610375530309 should equal 4660046610375530309
How come negative fibonacci numbers equal positive numbers?
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)
.