Yes, you are correct.
While similar they are still pretty different. They both have a "plug-and-play" feel, but differ in execution.
While I think your example is better suited for duck typing, I've written it out as one would using the strategy pattern.
function ColorStrategy(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
ColorStrategy.prototype.averageColor = function(){
return (this.r + this.g + this.b) / 3;
}
function colorClient(strategy){
return strategy.averageColor()
}
var red = new ColorStrategy(255, 0, 0);
var blue = {
r: 0,
g: 0,
b: 255,
averageColor: function(){
return (this.r + this.g + this.b) / 3;
}
}
colorClient(red);
colorClient(blue)
in this example it is bit more code doing it this way, we needed to define an extra function, colorClient, and we had to declare/assign averageColor twice (if we wanted to make it an object literal like you did, real world we wouldn't do this.)
Stragety pattern's advantage comes in to play when things are not all the same. for example, if our blue had an alpha value that we wanted to include in our average.
var alphaBlue = {
r: 0,
g: 0,
b: 255,
a: 0.5,
averageColor: function(){
return (this.r + this.g + this.b + this.a) / 4;
}
}
colorClient(alphaBlue)
Using the strategy pattern we can now define as many colors as we want. We are not limited to rgb, and we can become as abstract with our averageColor method as we need to and still get the value in exactly the same way.