Skip to content

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

@good-dev-student

Description

@good-dev-student

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 !

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions