I am trying to understand Object-Oriented JavaScript and stuck with some examples. They seems kind of different conventional OOP.
First of all, are there any difference with those:
function Foo()
{
this.bar = function ()
{
// ...
};
}
vs.
function Foo() {}
Foo.prototype.bar = function()
{
// ...
}
Also, should I use which one? Which one is proper way?
In another example for Factory Pattern.
function Circle()
{
this.say = function() { console.log('I am circle.'); };
}
function Square()
{
this.say = function() { console.log('I am square.'); };
}
function chooseShape(decision)
{
var shape = null;
if (decision === "Circle")
{
shape = Circle; // or Circle.prototype.constructor;
} else if (decision === "Square")
{
shape = Square; // or Square.prototype.constructor;
}
return new shape();
}
If I change those lines with these:
if (decision === "Circle")
{
shape = Circle.prototype.constructor;
} else if (decision === "Square")
{
shape = Square.prototype.constructor;
}
They are works as expected but I wonder what is going on under the hood.