Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: question 77 need clarification #369

Closed
RidgeA opened this issue Jun 15, 2020 · 4 comments
Closed

Discussion: question 77 need clarification #369

RidgeA opened this issue Jun 15, 2020 · 4 comments

Comments

@RidgeA
Copy link

RidgeA commented Jun 15, 2020

Question: https://github.com/lydiahallie/javascript-questions/blob/master/README.md#77-is-this-a-pure-function

I could imagine a situation where the function won't be pure.

let x = {
  [Symbol.toPrimitive]: () => {
    console.log('side effect');
    return 1
  }
}

let y = {
  [Symbol.toPrimitive]: () => {
    console.log('another side effect');
    return 2
  }
}

function sum(a, b) {
  return a + b
}

sum(x, y);

In this case, sum function will return 3, but all console.log are executed.
Obviously, this is a corner case, but....

@vinodkumartheking
Copy link

To best of my knowledge, a pure function is something which will return same result for same inputs, more kind of mathematical formula, !

@RidgeA
Copy link
Author

RidgeA commented Jun 27, 2020

That's true, but it is not enough.
Another mandatory property is the absence of side effects.
https://en.m.wikipedia.org/wiki/Pure_function

console.log is a side effect

Update:
Also, it is possible using the same trick with valueOf/Symbol.toPrimitive violate the first principle:

let x = {
  [Symbol.toPrimitive]: () => {
    console.log('side effect');
    return Math.random();
  }
}

@vinodkumartheking
Copy link

@RidgeA Yes, i agree. the above code now become not a pure function ! Thanks for the explaination.

@Lionad-Morotar
Copy link
Contributor

hhh~ I just wanted to create an issue.

image

Lionad-Morotar added a commit to Lionad-Morotar/javascript-questions that referenced this issue Jul 21, 2020
```js
const sum = (a, b) => a + b
// this is not a pure function, in some case, it could have side effects.
```

Lint to issue: lydiahallie#369

Need help: translate to english
@jakeherp jakeherp closed this as completed Sep 7, 2020
starwolf0320 added a commit to starwolf0320/technical-JavaScript-Questions1 that referenced this issue Aug 25, 2022
```js
const sum = (a, b) => a + b
// this is not a pure function, in some case, it could have side effects.
```

Lint to issue: lydiahallie/javascript-questions#369

Need help: translate to english
krismark930 added a commit to krismark930/javascript-skill-test that referenced this issue Jan 24, 2023
```js
const sum = (a, b) => a + b
// this is not a pure function, in some case, it could have side effects.
```

Lint to issue: lydiahallie/javascript-questions#369

Need help: translate to english
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants