Kae Job Sluder

16 August 2025: Reviewing JS Functions

Functions Review

I'm reviewing basic function and scope rules to help in working with some legacy code. I have my own style but I'm maintaining code that's written in a different style. See the Google Typescript Style Guide section on this for a suggestion to avoid using this and arguments.

Function Creation

Function Declaration is traditional. this is bound to parent object (Window in browser). Name is immediately available.

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

Function Expression: function defined but not directly declared with a name. Can only be used as an expression, typically assigned to something else. Similar to a lambda. this bound to context of invocation.

const add = function(a, b) {
	return a + b;
}

Arrow Functions: Concise lambda functions. No declared name. this bound to nearest ancestor at time of definition.

const add = (a, b) => a + b

Class Methods: Functions created as part of a class. this is bound to the created object.

class adder {
	add(a,b) {
		return a + b;
	}
}

Function Calling

The way functions are called can influence the behavior of the function by changing the context.

Takeaways