{"record":{"id":"ec518659b1c74598","repo":"karatelabs/karate","slug":"method-set-prototype-called-on-incompatible-receiver","errorCode":null,"errorMessage":"Method Set.prototype called on incompatible receiver","messagePattern":"Method Set\\.prototype called on incompatible receiver","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java","lineNumber":73,"sourceCode":"        // to integer, validate callability). Result-bearing methods build a\n        // fresh JsSet by populating elements directly (spec bypasses\n        // Set.prototype.add — verified by add-not-called.js test262).\n        install(\"union\", 1, this::union);\n        install(\"intersection\", 1, this::intersection);\n        install(\"difference\", 1, this::difference);\n        install(\"symmetricDifference\", 1, this::symmetricDifference);\n        install(\"isSubsetOf\", 1, this::isSubsetOf);\n        install(\"isSupersetOf\", 1, this::isSupersetOf);\n        install(\"isDisjointFrom\", 1, this::isDisjointFrom);\n        installConstructor(\"Set\");\n    }\n\n    private static JsSet asSet(Context context) {\n        Object thisObj = context.getThisObject();\n        if (thisObj instanceof JsSet s) {\n            return s;\n        }\n        throw JsErrorException.typeError(\"Method Set.prototype called on incompatible receiver\");\n    }\n\n    private Object add(Context context, Object[] args) {\n        JsSet s = asSet(context);\n        s.addValue(args.length > 0 ? args[0] : Terms.UNDEFINED);\n        return s;\n    }\n\n    private Object has(Context context, Object[] args) {\n        return asSet(context).has(args.length > 0 ? args[0] : Terms.UNDEFINED);\n    }\n\n    private Object delete(Context context, Object[] args) {\n        return asSet(context).deleteValue(args.length > 0 ? args[0] : Terms.UNDEFINED);\n    }\n\n    private Object clear(Context context, Object[] args) {\n        asSet(context).clearAll();","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java#L55-L91","documentation":"A Set.prototype method (add, has, delete, clear, etc.) was invoked with a `this` that is not an actual Set instance. The engine resolves the receiver with asSet() and throws a TypeError when it is anything else.","triggerScenarios":"Extracting a method and calling it unbound: const h = new Set().has; h(...); or Set.prototype.add.call(nonSet, v); or calling .has on undefined/null due to a chained lookup that silently returned undefined.","commonSituations":"Method borrowing from prototypes; passing a plain object or Map where a Set was expected; optional-chaining chains that skipped construction (e.g. maybeSet?.has(x) where maybeSet is not a Set).","solutions":["Call the method on a real Set instance created with new Set().","Use .call/.apply only with a genuine Set as the first argument.","Bind the method once and reuse the bound instance: const has = mySet.has.bind(mySet).","Validate with instanceof Set before invoking its methods."],"exampleFix":"// before\nconst has = mySet.has;\nhas(value);\n// after\nconst has = mySet.has.bind(mySet);\nhas(value);","handlingStrategy":"type-guard","validationCode":"if (!(s instanceof Set)) throw new TypeError('expected a Set');","typeGuard":"function isSet(v) { return v instanceof Set; }","tryCatchPattern":"try { return mySet.has(value); } catch (e) { if (String(e).includes('incompatible receiver')) { const s = new Set(); return false; } throw e; }","preventionTips":["Bind extracted methods immediately: const has = mySet.has.bind(mySet).","Use instanceof Set checks before calling Set.prototype methods via call/apply.","Avoid optional-chaining on values you have not confirmed are Sets."],"tags":["javascript","set","this-binding"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}