What I am trying to achieve is create some functions that operate on properties of an object in JavaScript, which is a fairly typical thing to do.
The problem is that in the interests of flexibility and extensibility I don't want the functions to know about the object in advance. As far as I can tell this is exactly the kind of thing that Javascript should be pretty good for, but not having spent a lot of time around functional languages I haven't yet got a handle on how to do it.
So I might have a situation like this:
function Owner()
{
var self=this;
self.size = 100;
self.writers = [];
}
function Writers()
{
var Alice= function()
{
var that=this;
that.name="alice";
that.operate = function()
{
console.log( "Alice says: "+self.size );
}
}
this.alice=new Alice();
}
var owner = new Owner();
var writers = new Writers();
owner.writers.push( writers.alice );
owner.writers[0].operate();
Now this returns Alice Says: undefined
which is all well and good, but not exactly what I am looking for- I would obviously like to see Alice Says: 100
. In a classical language I would probably have to hand the Alice function a reference to the owner ( in pseudo-C# ) :
public class Owner()
{
private List<Writer> writers;
public Owner()
{
this.size=100;
this.writers= List<Writer>();
}
public int Size{ get; set; }
public void AddWriter( Writer writer )
{
writer.Owner = this;
this.writers.Add(writer);
}
}
public class Writer
{
private Owner owner;
private string name;
public void Writer( string name )
{
this.name=name;
}
public void operate()
{
Console.WriteLine( "{0} says: {1}", name, owner.Size );
}
}
As far as I can tell, this is not very Javascripty- it seems like there should be a better- or at least more idiomatic - way of doing it.
In terms of the actual goal of the code I am working with ( which is basically like this, but more complicated ) the goal is to be able to add any number of "Writer" classes that can perform operations on the "Owner" instance to which they are attached. This is basically an implementation of the Decorator
pattern and I can think of a lot of ways I could implement this but as I say, I'm looking for understand how the functiony/prototypey nature of Javascript can help me in this kind of situation so I can get a better handle on the language.
I'm also not entirely happy with how I am passing around the functions through instances- I feel like I should be able to throw functions around the place like confetti, so any tips on how doing that interacts with variable scope would be very helpful.
If it turns out what I'm trying to do is ass-backwards and there is a better way of achieving this type of goal, that is fine too. I'm trying to get to the good language in Javascript and it's proving tricky but I'm learning a whole lot.
Edit: My goal is to have a base type where I don't have to know when I design it the complete list of operations it might have to perform and where I don't have to create a new subclass for every different combination of available operations, ensuring my operations are decoupled from my core types.
So if I have an Animal
type and then my operations were Eat
, Run
, Grow
and so on, I would ideally have a myAnimal.can("Eat")
type call ( I realise that this is a bit like hasOwnProperty
but I'm assuming that these operations are of a specific base type ) that would inform me whether that operation was available and then something like myAnimal.actions.Eat("leaves")
if it was accessible. When the Eat
action was called it would affect the properties of the parent myAnimal
object. During the lifetime of myAnimal
it might have abilities added or removed at any point, but as long as one checks that it can
do something that should be sufficient for my needs.