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

Suggestions to Improve Clarity in the Example of Passing State into Functions #14880

Open
good-dev-student opened this issue Jan 2, 2025 · 2 comments

Comments

@good-dev-student
Copy link

Hi there! I found the example of passing state into functions a bit complicated and unclear. It might help to simplify the explanation or break it down into smaller steps. I'd really appreciate it!

now:
https://svelte.dev/docs/svelte/$state#Passing-state-into-functions

function add(getA: () => number, getB: () => number) {
	return () => getA() + getB();
}

let a = 1;
let b = 2;
let total = add(() => a, () => b);
console.log(total()); // 3

a = 3;
b = 4;
console.log(total()); // 7

suggested:

function add(a: number, b: number) {
return a + b;
}

let a = 1;
let b = 2;

function total() {
return add(a, b);
}

console.log(total()); // 3

a = 3;
b = 4;

console.log(total()); // 7

or even :

<script lang="ts">
	const add = (a: number, b: number) => a + b;

	let a = 1;
	let b = 2;

	const total = () => add(a, b);

	console.log(total()); // 3

	a = 3;
	b = 4;

	console.log(total()); // 7
</script>

For the 3 versions i see the same result !

@Conduitry Conduitry transferred this issue from sveltejs/svelte.dev Jan 2, 2025
@FrotaLucas
Copy link

Hello everyone, I’d like to contribute to this task.
I really liked the two suggestions you provided, @good-dev-student . The example became much clearer! =)
Woud you mind if I submit a PR with your first suggestion?

@dummdidumm
Copy link
Member

Changing the code like that would change how the concept of closures is explained, and to me at least it becomes more confusing because there's now a new function introduced, whereas the current docs consistently talk about add as the only function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants