Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,829 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,860 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.