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 · 0 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
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

2 participants