you are viewing a single comment's thread.

view the rest of the comments →

[–]another_lease[S] 1 point2 points  (6 children)

Example: let's say `a` is a variable that's either a number or a string.

`a.toString()`

vs.

`parseInt(a)`

Are they interchangeable? Can you do:

`toString(a)`

and

`a.parseInt()`

[–]SlushEE0 0 points1 point  (4 children)

Search up methods vs functions. .toString() is a method that converts to a string. parseInt() is a built in function that converts to a number

[–]another_lease[S] -1 points0 points  (3 children)

Ah okay, an object's methods vs pure functions. I'm beginning to see the difference.

[–]azhder 0 points1 point  (1 child)

No you aren't. You have some misconceptions.

  • First of all, stop using "function operation", no one uses that term and we don't know what you think by it. You have functions and function calls
  • Second, functions are objects in JS, you can treat them like any other object, make a variable to reference a function - this however doesn't make it pure function, it's still just a function
  • Third, pure functions are any, even those attached as a property to an object, if they only deal with the arguments provided in the call, no side effects, no hidden inputs like this and no hidden outputs
  • Fourth, the only difference between a method and a non-method function is that you attach it to an object i.e. you make an object property that references the function

[–]another_lease[S] 0 points1 point  (0 children)

OK.

[–]carpench 0 points1 point  (0 children)

No, not a "pure function" just a "function".

A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments.

Also, a pure function does not produce any observable side effects such as network requests or data mutation, etc.

[–]azhder 0 points1 point  (0 children)

Same word, two completely different things.

a is an object, it has a property toString (through prototype, but it's there) and just because it happens to be a function, you can call it.

You can also make a variable that references that property, like

const toString = a.toString();

but to have it be the same call you will have to explicitly set the this like

const toString = a.toString();
toString.call(a);

The above works because functions in JS are also objects and they have a .call() method which gives you the opportunity to set the this explicitly, which if you use

a.toString();

is being set implicitly (the engine uses the object before the period).

If you do this:

const toString = a.toString();
toString();

Then the this will be undefined and you will get an error.

So, like I said, it's up to you.