Advanced function patterns including declaration styles, closures, scope chains, hoisting, and this binding. Master function composition and advanced techniques.
Provides advanced JavaScript function patterns including closures, scope chains, `this` binding, and hoisting. Use when analyzing or writing complex functions, debugging scope/`this` issues, or implementing patterns like currying, memoization, and factory functions.
/plugin marketplace add pluginagentmarketplace/custom-plugin-javascript/plugin install javascript-developer-plugin@pluginagentmarketplace-javascriptThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlassets/function-patterns.yamlreferences/CLOSURES-GUIDE.mdreferences/GUIDE.mdscripts/analyze-functions.jsscripts/helper.py// Declaration (hoisted)
function greet(name) { return `Hello, ${name}!`; }
// Expression (not hoisted)
const greet = function(name) { return `Hello, ${name}!`; };
// Arrow (lexical this)
const greet = (name) => `Hello, ${name}!`;
const greet = name => `Hello, ${name}!`; // Single param
const getUser = async (id) => await fetch(`/api/${id}`);
Global Scope
└── Function Scope
└── Block Scope (let/const)
const global = 'accessible everywhere';
function outer() {
const outerVar = 'accessible in outer + inner';
function inner() {
const innerVar = 'only accessible here';
console.log(global, outerVar, innerVar); // All work
}
}
function createCounter() {
let count = 0; // Private state
return {
increment: () => ++count,
decrement: () => --count,
get: () => count
};
}
const counter = createCounter();
counter.increment(); // 1
counter.increment(); // 2
| Context | this Value |
|---|---|
| Global | window/global |
| Object method | The object |
| Arrow function | Lexical (outer) |
call/apply/bind | Explicit value |
Constructor (new) | New instance |
// Explicit binding
fn.call(thisArg, arg1, arg2);
fn.apply(thisArg, [args]);
const bound = fn.bind(thisArg);
// IIFE (Immediately Invoked)
const module = (function() {
const private = 'hidden';
return { getPrivate: () => private };
})();
// Currying
const multiply = a => b => a * b;
const double = multiply(2);
double(5); // 10
// Memoization
function memoize(fn) {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (!cache.has(key)) cache.set(key, fn(...args));
return cache.get(key);
};
}
| Problem | Symptom | Fix |
|---|---|---|
Lost this | undefined or wrong value | Use arrow fn or .bind() |
| Closure loop bug | All callbacks same value | Use let not var |
| Hoisting confusion | Undefined before declaration | Declare at top |
| TDZ error | ReferenceError | Move let/const before use |
// BUG: var is function-scoped
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 3, 3, 3
// FIX: Use let (block-scoped)
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// Output: 0, 1, 2
// 1. Check this context
console.log('this is:', this);
// 2. Verify closure captures
function test() {
let x = 1;
return () => { console.log('x:', x); };
}
// 3. Check hoisting
console.log(typeof myFunc); // 'function' or 'undefined'?
function createLogger(prefix) {
return {
log: (msg) => console.log(`[${prefix}] ${msg}`),
error: (msg) => console.error(`[${prefix}] ${msg}`)
};
}
const apiLogger = createLogger('API');
apiLogger.log('Request received');
function debounce(fn, delay) {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
}
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.