Setting the context of methods is just a convenience.
One option, stack-based method tracking:
hd keeps a stack of scopes for methods
a new model construction pushes a new scope
each method constructed gets added to the current scope
after the model is constructed, the context of each method in the top scope is set to the model, and the scope is popped
Second option, stack-based context tracking:
hd keeps a stack of contexts
a new model construction creates an empty object, pushes it as the context, and calls the constructor with the object as context
each method constructed gets the top context
after the model is constructed, the top context is popped
Second option is more efficient, but will not support case where constructor returns a model object rather than using this.
For model literals, for each property that is a derived variable, its method's context is set to the model.
What about a variable constructed in one place and used in several others:
var x = hd.computed(...);
hd.bind(hd.model({ one: x }), $("#one"));
hd.bind(hd.model({ two: x }), $("#two"));
The context of the method will be changed. Is this even a real-world possibility? If a variable is included in two different models, it probably isn't using the context.
Setting the context of methods is just a convenience.
One option, stack-based method tracking:
Second option, stack-based context tracking:
Second option is more efficient, but will not support case where constructor returns a model object rather than using
this
.For model literals, for each property that is a derived variable, its method's context is set to the model.
What about a variable constructed in one place and used in several others:
The context of the method will be changed. Is this even a real-world possibility? If a variable is included in two different models, it probably isn't using the context.