Home / Function/ bind_this() — svelte Function Reference

bind_this() — svelte Function Reference

Architecture documentation for the bind_this() function in this.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  ce2fa74c_287c_b099_4a87_8b97b486275c["bind_this()"]
  74650c92_257d_4a26_2826_31c4edae73a3["this.js"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|defined in| 74650c92_257d_4a26_2826_31c4edae73a3
  a985ae40_8ef8_7ef2_adad_116fbf97e70c["effect()"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|calls| a985ae40_8ef8_7ef2_adad_116fbf97e70c
  7494b934_a3b8_689e_91b6_8435e26461c5["render_effect()"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|calls| 7494b934_a3b8_689e_91b6_8435e26461c5
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack()"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|calls| a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  805292aa_dddc_7816_26f0_84356e6181e6["is_bound_this()"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|calls| 805292aa_dddc_7816_26f0_84356e6181e6
  2cab0f64_6d19_d981_66e2_d2555c252702["queue_micro_task()"]
  ce2fa74c_287c_b099_4a87_8b97b486275c -->|calls| 2cab0f64_6d19_d981_66e2_d2555c252702
  style ce2fa74c_287c_b099_4a87_8b97b486275c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/elements/bindings/this.js lines 25–61

export function bind_this(element_or_component = {}, update, get_value, get_parts) {
	effect(() => {
		/** @type {unknown[]} */
		var old_parts;

		/** @type {unknown[]} */
		var parts;

		render_effect(() => {
			old_parts = parts;
			// We only track changes to the parts, not the value itself to avoid unnecessary reruns.
			parts = get_parts?.() || [];

			untrack(() => {
				if (element_or_component !== get_value(...parts)) {
					update(element_or_component, ...parts);
					// If this is an effect rerun (cause: each block context changes), then nullify the binding at
					// the previous position if it isn't already taken over by a different effect.
					if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) {
						update(null, ...old_parts);
					}
				}
			});
		});

		return () => {
			// We cannot use effects in the teardown phase, we we use a microtask instead.
			queue_micro_task(() => {
				if (parts && is_bound_this(get_value(...parts), element_or_component)) {
					update(null, ...parts);
				}
			});
		};
	});

	return element_or_component;
}

Domain

Subdomains

Frequently Asked Questions

What does bind_this() do?
bind_this() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/elements/bindings/this.js.
Where is bind_this() defined?
bind_this() is defined in packages/svelte/src/internal/client/dom/elements/bindings/this.js at line 25.
What does bind_this() call?
bind_this() calls 5 function(s): effect, is_bound_this, queue_micro_task, render_effect, untrack.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free