last day (15 days later) » 

9:31 AM
0
A: using function caller names in method chaining

RajeshYou can add a property in child classes and based on this value, you can return this. Sample // Parent Class function Pet() { this.sec = 1000 }; Pet.prototype.run = function takeoff(cb, sec) { setTimeout(function() { //cb(); console.log('Run'); }, sec || this.sec); ...

 
thanks for your help. how would you modify cat methods without modfying Pet methods? Also, because of the setTimeout, it seems that it is not in order. Let's say if sec in comeback = 100, and sec in run = 500, it will print Come back before Run
 
If you wish to override, just add that method in child class prototype. Also, setTimeout will register an event at specific tick. Thats it. processing of functions at same tick is take care of by compiler and you cannot force it. You can either get rid of setTimeouts or keep a counter to add delay
 
OK, I tried to override the child prototype class, as edited above: index.html:101 Uncaught TypeError: Cannot read property 'bark' of undefined
 
Because you are not returning anything.
 
I thought return that serves that purpose? how would you fix it for it to return this object?
 
9:31 AM
Can you share a sample JSFiddle where I can debug your code?
 
Have you checked what cat looks like on console?
your function is not called.
If you wish to use prototype, you should use function as class and not override/create prototype on object
 
This is working.
(Without touching the object Pet and without the use of eval)
 
9:46 AM
well the order is off
it's supposed to print out 'Run, bark, comeback'
 
yes but this is something that rajesh pointed out
 
according to the order that is called and not the window settimeout
 
would you remove the setTimeout and call console.log directly, it would work
 
naw...i wouldn't be able to modify anything from the pet() class
 
to get the order correct with setTimeout, setTimeout could fullfill a promise that you return
 
9:48 AM
that has to stay intact
 
maybe what you actually want are promises
 
yeah
 
First I dont think there is a need of setTimeout
second, instead of creating own object and mapping every prototype function to it, use prototype
 
you mean in the pet class?
that one is required and has to be there. i can only modify cat
 
9:50 AM
I have already mentioned that OP is overriding instance (cat)
cat is an Object and not a class. prototype is similar to interfaces in Java/c#
 
yes i understand that
 
yes, but the solution he implemented himself is method overloading and as far as I understood he wanted a generic way to do this. The setTimeout calls are another thing and there Promises will be useful
his inherited function will need to create and fullfill promises in order
 
let's get the timer issue resolved first
the other is a bonus answer, so i wouldn't have to deal with it right now
 
For the Timers use Promises, and resolve them in the function that got called
 
Whats the order of console?
 
10:03 AM
it's based on the window's timer
 
your delay is variable: 1000, 500, 750.
even I'd suggest promise
 
cat.run().bark().comeBack();
i want it to be able to print out run/bark/comeback and not the window timer
it should wait in that order
 
Then you should use common delay
Then you should use common delay
 
what do you mean?
i check the timer every time?
it seems like i can compare how much time left
after each child is kicked in
 
10:28 AM
Where does the code for the setTimeout need to be?
Inside Pet or could it be inside Cat?
 
it is inside pet
pet is what cannot be changed
 
okay
 
i have to rewrite cat so that it accommodates the asynchronosity of Pet() :(
 
Are you able to modify the logic of callback()?
probably you are right?
 
no
what do you think of using promises like this one? as modified in the run method
for cat
i feel like im close but it's 4am here so i'm dead tired
 
10:49 AM
What do you thing about this?
the problem is, that the logic for fullfilling the promise needs to go into the callback
:X
Your code is almost correct, but if you return the promise, then you do not need to return this
ahh no
Inside your promises you are assigning the functions
Promises work like this: You create the object by calling a function that might call the resolve function in the future
and as soon as the resolve function is called, the function that you put in then will be executed
So you want to call Run, which should call resolve somehow after console.log('Run')
not after setting the function =)
 
 
5 hours later…
3:31 PM
wow. thanks toaogg.
i have a lot to learn writing pure JS functions
 
 
6 hours later…
9:31 PM
01222222222222222222222224
 

last day (15 days later) »