Let's see two examples in which I'll try to explain what I want to understand.
var Car = function(){
// Init class
function Car() { };
// Private func/vars
var private = { color:'red' };
// Public func/vars
Car.prototype = {
newColor: function(color) { private.color = color },
getColor: function() { return private.color }
};
return Car.prototype; // return with prototype
};
var myCar = new Car();
And:
var Car = (function(){
// Init class
function Car() { };
// Private func/vars
var private = { color:'red' };
// Public func/vars
Car.prototype = {
newColor: function(color) { private.color = color },
getColor: function() { return private.color }
};
return Car; // avoid prototype adding parentheses on next line;
})();
var myCar = new Car();
Let's see!, Both class are created as function expression and both work equally. The only differences between them, are: The first return the Car function with its prototype property. The second works returning the Car function, avoiding the prototype property and instead use IIFE.
What's the differences between use return Car.prototype;
and avoid IIFE and use return Car;
using IIFE (parentheses at the end of the class declaration).
new
operator with both of theCar
objects returned ? – Augustus