The two approaches that you describe are actually completely equivalent (apart from the syntax) and mostly endorsed from a Javascript point of view. Nevertheless, you probably should not choose what to do, based only on what is convenient for you now - in the long run you will need to get used to working with the language, and not against it. Moving on ...
What if I want my fields to be of a specific type, like in Java?
Javascript is dynamically typed, so it will be difficult for you to try to apply the same static typing paradigm. You can try to execute runtime checks (using typeof) during the constructor of objects, but this is usually not worth the trouble, since the verification will still be performed at runtime, but the verification will not lead to a similar error in any case, and typeof is very limited (it’s difficult to verify that something is an array, it is annoying to check the interfaces and allows you to not run in the “alien” objects of the browser ...)
In the end, it’s not very tense with the dynamic typing - you will soon get used to it.
If you said that the object literal approach and the constructor function approach return the same result, what is the difference?
First of all, while object literals are a very neat part of the syntax, there are some things that need to be divided into several statements, so you need a function for them:
//Note: lowercase name since I won't be using 'new here... //there is a good convention for only using capital names on // "real" constructors function create_human(name, age){ var obj = {}; obj.name = name; obj.age = age; //this needs to be on a separate statement //since it involves the other fields obj.isAdult = (obj.age >= 21); return obj; } //not using 'new ...yet var that_penguin = create_human("Lenny", 42);
Note that object literals can still be very useful here, and it is very popular to use them to provide named parameters and default parameters in cases where normal ones have large parameter lists:
function create_human(args){ var obj; obj.name = args.name;
Remember:. Up to this point, using a function to create an object and an object literal is only a matter of style and organization, and the best approach usually depends on which particular case you are dealing with. In my experience, object literals are very good for creating singleons and configuration dictionaries, and functions are very useful for providing invariants in complex objects, offering a shorthand notation for general ones. p>
So, what is the deal with the “real” design functions, new and then?
The disadvantage of explicitly manipulating objects manually is that we are missing out on some of the OO kindness we are used to. Providing each object with a copy of its methods, we not only lose space (in the classical language they will be stored in the class), but we lose differential inheritance (since everything is static). The way Javascript deals with this is the prototypes. All objects have a prototype and when searching for a property (or method), it is recursively scanned in the prototype if it is not found immediately.
A common example of using prototypes is that the class of objects saves its instance variables on its own, but shares methods:
lenny: name: "Lenny" age: 42 __proto__: Person.prototype glenda: name: "Glenda" age: 19 __proto__: Person.prototype Person.prototype: printInformation: ... tons of methods: ...
Thus, we can access lenny.printInformation without even noticing that this method is shared with Glenda.
To create an object with a prototype, you can either use Object.create (at least for newer browsers), or in the old way using the function constructor and the new operator:
function Person(name, age){
Summarizing
Use the constructor functions and the new operator if you want to use the prototype functions of the language.
Use regular functions or object literals otherwise.