{"record":{"id":"1384bb87e2f93893","repo":"phaserjs/phaser","slug":"initialize-must-be-a-function","errorCode":null,"errorMessage":"initialize must be a function","messagePattern":"initialize must be a function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/Class.js","lineNumber":193,"sourceCode":" *          }\n *      });\n */\nfunction Class (definition)\n{\n    if (!definition)\n    {\n        definition = {};\n    }\n\n    //  The variable name here dictates what we see in Chrome debugger\n    var initialize;\n    var Extends;\n\n    if (definition.initialize)\n    {\n        if (typeof definition.initialize !== 'function')\n        {\n            throw new Error('initialize must be a function');\n        }\n\n        initialize = definition.initialize;\n\n        //  Usually we should avoid 'delete' in V8 at all costs.\n        //  However, its unlikely to make any performance difference\n        //  here since we only call this on class creation (i.e. not object creation).\n        delete definition.initialize;\n    }\n    else if (definition.Extends)\n    {\n        var base = definition.Extends;\n\n        initialize = function ()\n        {\n            base.apply(this, arguments);\n        };\n    }","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/utils/Class.js#L175-L211","documentation":"Thrown by the Phaser.Class factory when the definition object has an `initialize` key whose value is not a function. `initialize` is the reserved slot for the ES5-style constructor; Phaser requires it to be callable so it can be invoked on instantiation. Any other type (string, number, object, array, null when truthy-checked via property access) is rejected up front rather than failing later at `new`.","triggerScenarios":"Passing `new Phaser.Class({ initialize: 'not a function' })`, `{ initialize: 42 }`, `{ initialize: { foo: 1 } }`, or `{ initialize: [function(){})] }`. Also from accidentally assigning a class reference or a config object to initialize instead of an actual function, or from a build step that serializes the definition and leaves initialize as a string.","commonSituations":"Refactoring a class and renaming the constructor without updating the initialize reference. Copy-pasting a class template and forgetting to replace the placeholder value. Transpiling/bundling where tree-shaking removes the function but leaves the key. Mixing ES6 class syntax (which has a real constructor) with Phaser.Class and assigning the wrong thing.","solutions":["Ensure definition.initialize is a function declaration or expression: `initialize: function () { ... }`.","If you do not need a custom constructor, omit initialize entirely; Phaser will synthesize one (delegating to Extends if present).","Check for typos: the key must be exactly 'initialize' (American spelling).","If migrating from ES6 class, map `constructor` to `initialize` explicitly."],"exampleFix":"// before\nvar Enemy = new Phaser.Class({\n  initialize: 'spawnEnemy',\n  hp: 10\n});\n\n// after\nvar Enemy = new Phaser.Class({\n  initialize: function (hp) { this.hp = hp; },\n  hp: 10\n});","handlingStrategy":"type-guard","validationCode":"function safePhaserClass(definition) {\n  if (definition && 'initialize' in definition && typeof definition.initialize !== 'function') {\n    delete definition.initialize;\n  }\n  return new Phaser.Class(definition);\n}","typeGuard":"function isValidClassDefinition(d) {\n  return !d || !('initialize' in d) || typeof d.initialize === 'function';\n}","tryCatchPattern":"try {\n  Klass = new Phaser.Class(definition);\n} catch (e) {\n  if (e.message === 'initialize must be a function' && typeof definition.initialize !== 'function') {\n    delete definition.initialize;\n    Klass = new Phaser.Class(definition);\n  } else { throw e; }\n}","preventionTips":["Spell the key 'initialize' (American) and assign a function expression, not a name reference.","Run a lint rule that flags non-function values on the reserved 'initialize' key.","If no custom constructor is needed, omit initialize entirely.","When migrating ES6 classes, map `constructor` -> `initialize` explicitly, do not copy the class reference itself."],"tags":["class","constructor","initialization","type-error","oop"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}