-1
console.log(Object.prototype.hasOwnProperty('parseInt'));  // false
console.log(Number.prototype.hasOwnProperty('parseInt'));  // false

Where is parseInt defined if not on the default prototype object (Object.prototype), the prototype object from which all JS objects inherit?

noSelf_
  • 29
  • 5
  • 3
    `global`, aka `window` (in the DOM in web-browsers) - run `typeof window.parseInt` in your browser's devtools console to see for yourself. – Dai Aug 15 '22 at 06:47
  • 1
    The `prototype` of `Object` is not `window`, that's why. – Dai Aug 15 '22 at 07:38
  • @Dai But why can't I use something like `Object.getPrototypeOf(Object).hasOwnProperty('parseInt')` to verify it's existence on global ? – noSelf_ Aug 15 '22 at 07:44
  • 2
    Because `Object.getPrototypeOf(Object)` **is not** `window`. (The expression `Object.getPrototypeOf(Object)` actually returns a `Function` object, which is the built-in `Object` ctor function) – Dai Aug 15 '22 at 07:47
  • 2
    @noSelf Can you explain why you believe that this _should_ work? Because you seem to be mixing up different concepts. – Ivar Aug 15 '22 at 07:49
  • @Ivar hmm. So if I understand it correctly, we have a global object which is everywhere in the JS program. Then we have the built in Object constructor, Function constructor, Array constructor etc with their own prototypes (containing methods like forEach etc) `within` this global object ? – noSelf_ Aug 16 '22 at 03:34
  • 1
    @noSelf_ Pretty much yes. The prototypes are a mechanism that allows objects to inherit features form other objects. The functions that are in the prototype of `Number` can directly be called on numbers, like `1.1.toFixed(2)`. This works because `.toFixed()` exists on the `Number.prototype`. You don't call `parseInt()` on a number like `(1).parseInt('123')`. See [Object prototypes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes) on MDN. – Ivar Aug 16 '22 at 09:15
  • @Ivar Thanks for the reply. Just out of curiosity. Is there a way to make `Number` type inherit the `parseInt()` from global ? – noSelf_ Aug 17 '22 at 01:52
  • 1
    You can assign the `parseInt` function to the `Number` prototype with `Number.prototype.parseInt = parseInt`. Though it doesn't make much sense because you already need to have an instance of a number type, which defeats the purpose of parsing one. Here is an example: https://jsfiddle.net/ozu4hj8g/ – Ivar Aug 17 '22 at 06:48
  • @Ivar Aha... learnt something new today `Number.prototype.parseInt = parseInt`. Although this particular eg is not useful :) but still might come in handy sometime. Thank you very much!! – noSelf_ Aug 17 '22 at 09:02
  • 1
    @noSelf_ I do want to point out that it is [generally considered bad practice to extend the prototype of build-in objects](https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice). There are useful applications, such as [polyfills](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill), but in general it can cause bugs that are very hard to discover. – Ivar Aug 17 '22 at 09:08

1 Answers1

1

Both parseInt and parseFloat are native code, so they will depend on the implementation. Neither function has a prototype.

Somewhere in this commit, there are some definitions for GlobalParseInt and GlobalParseFloat. If you have knowledge of C/natives, you could take a look and learn for yourself. As for Spidermonkey, Firefox's engine, I couldn't find an exact file but did find many references to the parseInt function in this search.