So when i declare an array as var arr = []
in browser console and then do arr.__proto__
i get the prototype in console.
But when i do the same thing in nodeJs i get []
as result.
Nodejs is javascript running on server so why is the result not same . Am i missing something ? How do i access the prototype in nodeJs and get the same result.
What you are seeing is simply a quirk due to display differences. arr.__proto__
points to the exact same object in both cases - Array.prototype
- it's just that your browser and Node present the object to the user for display differently.
You already can access, for example, arr.__proto__.fill
in Node, even though it's not displayed in the console when logging arr.__proto__
. You can iterate over the properties manually to display them if you want:
const arr = [];
Object.getOwnPropertyNames(arr.__proto__).forEach((key) => {
console.log(key);
});
The above will show you the exact same results in both environments.
If you hook up Node to Chrome's console, you'll see that console.log(arr.__proto__)
looks exactly the same when looking at the log from Node in Chrome, and when looking at the log from a webpage in Chrome.
But, do note that __proto__
is deprecated - you should prefer to use Object.getPrototypeOf
nowadays.