{"record":{"id":"798cd2722b71e9b6","repo":"nwjs/nw.js","slug":"it-s-forbidden-to-instantialize-a-base-class","errorCode":null,"errorMessage":"It's forbidden to instantialize a Base class.","messagePattern":"It's forbidden to instantialize a Base class\\.","errorType":"exception","errorClass":"String","httpStatus":null,"severity":"error","filePath":"src/api/base/base.js","lineNumber":24,"sourceCode":"//  in the Software without restriction, including without limitation the rights\n//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co\n// pies of the Software, and to permit persons to whom the Software is furnished\n//  to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in al\n// l copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM\n// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES\n// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS\n//  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH\n// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n//  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nvar v8_util = process.binding('v8_util');\n\nfunction Base() {\n  throw new String(\"It's forbidden to instantialize a Base class.\");\n}\nrequire('util').inherits(Base, require('events').EventEmitter);\n\n// Silent unhandled events\nBase.prototype.handleEvent = function() {\n  this.emit.apply(this, arguments);\n}\n\n// Generic getter and setter\nBase.prototype.handleGetter = function(name) {\n  return v8_util.getHiddenValue(this, 'option')[name];\n}\n\nBase.prototype.handleSetter = function(name, setter, type, value) {\n  value = type(value);\n  v8_util.getHiddenValue(this, 'option')[name] = value;\n  nw.callObjectMethod(this, setter, [ value ]);\n}","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/nwjs/nw.js/blob/e15da848e9e08e6e467532dae78995c6ad2f55ee/src/api/base/base.js#L6-L42","documentation":"Base is the abstract root class for every nw.js API object (App, Menu, MenuItem, Clipboard, Screen, Shortcut). Its constructor unconditionally throws, so `new (require('nw.gui').Base)()` never produces an instance. Note the thrown value is `new String(...)`, a String object rather than an Error — so `instanceof Error` is false in a catch block. The message also contains a typo (\"instantiate\").","triggerScenarios":"Directly instantiating Base: `new nw.Base()`. Indirectly: a subclass whose constructor forgets to allocate the native object and falls through, or code that reaches Base via a misconfigured prototype chain. Also possible if a user attempts to subclass nw API classes and calls Base.call(this).","commonSituations":"Developers exploring the API surface try to construct Base directly, or copy-paste the inheritance pattern (`require('util').inherits(MyClass, nw.Base)`) without providing their own constructor that calls nw.allocateObject. Inheritance misuse during framework authoring.","solutions":["Do not instantiate Base; use a concrete subclass (nw.App, new nw.Menu(), new nw.MenuItem(opt), new nw.Shortcut(opt)).","If subclassing, give your type a constructor that calls nw.allocateObject(this, option) before inheriting from Base.","If you only wanted EventEmitter behavior, inherit from require('events').EventEmitter instead of nw.Base."],"exampleFix":"// before\nvar b = new nw.Base(); // throws\n\n// after\nvar menu = new nw.Menu({ type: 'contextmenu' }); // concrete subclass","handlingStrategy":"validation","validationCode":"// Base is abstract — never construct it. Validate intent before any nw.Base use:\nif (obj === nw.Base || (obj && obj.constructor === nw.Base)) {\n  throw new Error('Use a concrete nw API class, not Base.');\n}","typeGuard":"function isAbstractBase(v) {\n  return v === undefined || (v && v.constructor && v.constructor.name === 'Base');\n}","tryCatchPattern":"try { var x = new nw.Base(); }\ncatch (e) {\n  // Note: thrown value is a String object, not an Error\n  if (typeof e === 'object' && e instanceof String && /forbidden/.test(String(e))) {\n    console.error('Do not instantiate nw.Base; use a concrete class.');\n  } else { throw e; }\n}","preventionTips":["Treat nw.Base as internal; never reference it in app code.","When subclassing, always provide a constructor that calls nw.allocateObject.","For EventEmitter-only behavior, inherit from require('events').EventEmitter."],"tags":["nwjs","base","abstract-class","internal","programming-error"],"backgroundTag":null,"analyzedSha":"e15da848e9e08e6e467532dae78995c6ad2f55ee","analyzedAt":"2026-08-13T04:15:35.452Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}