Reputation: 11830
I was playing with Javascript and there is this one with thing which is puzzling me..
Suppose we have two functions
function A (argA) {
//Do Something with argA
return somehingFromA
}
function B (argB) {
//Do somwthing with argB
return somethingFromB
}
if we want to pass return of A to B, we would probably do this B(A(arg))
this should give return of function A to B to process?
Now, when we do something like this
arr.split(' ').join('-')
we are also pasing the return from split
to join
?
I know they both aren't equal, can someone tell me what I am thinking wrong?
Upvotes: 0
Views: 837
Reputation: 86
For. Split(), It returns array. For join syntax is "array you want to join". Join(separator).
As array extend function join you are using this way.
Some functions need parameter but some datatype extend function to apply some operations.
I hope that clears your doubt
Upvotes: 1
Reputation: 1074258
I do see why you're confused, it is slightly subtle.
When you do B(A(arg))
, you're passing A
's return value into B
. B
sees that value as its parameter argB
.
When you do arr.split(' ').join('-')
you're using split
's return value (an array) by calling a method on it, instead of passing the return value into join
as an argument. join
doesn't see the array as a parameter at all. (It does see it as this
, because in the normal case when you do obj.method()
, this
within the call to method
has the same value obj
had. Which is what makes this slightly subtle.)
Upvotes: 2
Reputation: 1785
Methods are being applied to some objects. In your example .split() returns an array. join() is applied to that new array.
Upvotes: 0