Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing Object Properties

Aaron Munoz
Aaron Munoz
11,177 Points

Is Javascript really OOP?

I ask this because I've learned OOP with PHP and now that I'm learning JS I'm somewhat confused. It seems like JS objects are just arrays with custom keys .PHP can make Arrays with custom keys as well as Objects. What's the difference?

2 Answers

Kristopher Van Sant
PLUS
Kristopher Van Sant
Courses Plus Student 18,830 Points

The short answer: Yes JavaScript is OOP.

Long answer: "OOP (Object-Oriented Programming) is an approach in programming in which data is encapsulated within objects and the object itself is operated on, rather than its component parts. JavaScript is heavily object-oriented. It follows a prototype-based model (as opposed to class-based)." https://developer.mozilla.org/en-US/docs/Glossary/OOP

" Object-oriented to the core, JavaScript features powerful, flexible OOP capabilities." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

"JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Here are a couple of resources discussing what objects in JavaScript are and how they differ from arrays.

Differences between Arrays and Objects in JavaScript - https://www.quora.com/What-are-some-differences-between-arrays-and-objects-in-Javascript

Is everything an object in JS? http://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object

Colin Marshall
Colin Marshall
32,861 Points

The objects you are talking about are called object literals in JavaScript.

For object oriented programming in JavaScript you use the constructor/prototype pattern. There is a course here on Treehouse called Object Oriented JavaScript that goes over this.