2

Might be a silly question, but what is more logical:

object.actions.bark() or object.action.bark()?

Simply about plurality. I know arrays are usually pluralized. But if I were to be consistent; ideally, objects should be singular in nature, right?

I guess you could say my example was more of an associative array rather than an object. But doesn't an action object possess properties like bark(), much like how object object possesses the action property.

PS:

This is not opinion-based! It's a very objective inquiry on quality, documentation, and optimization. Imagine the chaos if this was subjective!

asked Apr 17, 2017 at 8:32
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 17, 2017 at 11:42

1 Answer 1

3

OK, so javascript has an oddity (amongst many) in that objects are in a way also collections.

But this shouldnt affect your naming. In OOP you should generally try to name objects as they would be called in real life and the Methods of those objects are things the object can do.

so:

dog.Bark()
cat.Meiow() 

etc

now you might have a complex object with sub objects or collections of objects. But again the naming should map to the real thing.

company.employees[5].fire()

It would be odd to create an object and use it like a collection

company.employees.bobsmith.fire()

even though such a thing is (in JS) equivalent to a collection of key value pairs

company.employees['bobsmith'].fire()

If you are going to write OO code in javascript its best to pretend that its strongly typed even though its not.

answered Apr 17, 2017 at 10:07
2
  • So you're saying that company.employees is a collection??? Commented Apr 17, 2017 at 10:27
  • 2
    everything is a collection in JS, your only choice is the type of collection Commented Apr 17, 2017 at 10:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.