JS: host vs. native objects

Published on: November 16, 2014

Tags: js and interview-questions

What’s the difference between host objects and native objects?

I didn’t find as much lot of information about this topic as some of the other JS interview questions. So here’s my current understanding, but please comment if I've misunderstood or there’s a better way to explain it.

What’s a native object?

Native objects are inherent to JS - they are available to you so long as you're using JS. You can be in the browser or in Node, but if you're writing JS, you've got access to the native objects. Here’s a list of them if you want to know what’s available to you.

What’s a host object?

Everything the environment gives you. For the browser, this includes objects like window. Host objects can differ by environment (or host), so that Node wouldn’t have access to window (which makes sense since there’s no DOM for Node), but could have its own host objects like NodeLists.

What’s a user object?

This is where I got the most confused. The question asks for the difference between host and native objects, which was covered on a few sites. However, a couple of sites make a third category: user objects. There seems to be some debate about user objects being their own category. I’m not sure which side is right so I’m adding a little blurb about them here, but please ignore if it’s not relevant.

User objects are anything the user defines. When you create a new object that is not directly a native object, you've made a user object. So if you create a new string ("Hello world") you created a native object, but if you create an instance of an object you've defined (new Cat()) then it’s a user object.

References


comments powered by Disqus