0

I need to understand why the following is working, to ensure it's always working:

asyncCall = function(value) {
    returnValue = callSecondFn(value);
    (returnValue === value) //is this always true
}

callSecondFn(value) {
    delete value['key'];
    return value;
}

is this always given? Can you explain why?

Dinkheller
  • 4,631
  • 5
  • 39
  • 67
  • 7
    You pass in an object, which is then returned. You then compare the two -- and it's of course still the same object! – Cameron Oct 01 '14 at 12:23
  • 1
    nothing to do with your problem. but u miss a semicolon. – Mithun Satheesh Oct 01 '14 at 12:24
  • The line `(returnValue===value)` does nothing and asyncCall has no return value, if it needs one... Javascript doesn't return the last expression, like some languages do. But it is impossible to return anything when doing async coding, so I have no idea what you are trying to here. Did you intend to return a true/false? `asyncCall` also contains no async code. Do you intend to use it with an ajax call not included? – Paul Oct 01 '14 at 12:31
  • @Paul: this is just a demo code. In my code this snippet comes from the success function. I call another function from within doing something alike callSecondFn. I wanted to find out, if returnValue will be always (=== value) at the point of my comment. I wanted to know if this will always work. – Dinkheller Oct 01 '14 at 15:38

3 Answers3

2

The point to note is that here returnValue and value are two references pointing to the same object. You passed the object reference to the function which modified it via the reference by removing a property from the object. So the object got altered. But returnValue and value are equal as they both point to the same resulting object.

asyncCall = function(value) {
    returnValue = callSecondFn(value);
    console.log(value,returnValue,returnValue === value); 
    //see both have lost key attribute
};

asyncCall({k:1,key:3});

function callSecondFn(value) {
    delete value['key'];
    return value;
}
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
1

It's working, because in js objects are passed by reference. This means that 'value' and 'returnValue' both refer to the same object.

kihu
  • 842
  • 5
  • 13
0

Thanks both of you (mithunsatheesh + kihu), but I found the best answer to my question after you wrote about passed by value and by object.

So here is what I found is the best answer to my question:

LINK stackoverflow

LINK external

Thanks again for pointing me into the correct direction

Community
  • 1
  • 1
Dinkheller
  • 4,631
  • 5
  • 39
  • 67