setup
construct.setup(...args)
A setup function for the instantiation of a constructor function.
Parameters
- args
{*}
:The arguments passed to the constructor.
Returns
{Array|undefined|ReturnValue(value)}
:
If an array is returned, the array's items are passed as arguments to init. If a ReturnValue instance is returned, the ReturnValue instance's value will be returned as the result of calling new Construct(). The following example always makes sure that init is called with a jQuery wrapped element:
WidgetFactory = Construct.extend({
setup: function(element){
return [$(element)]
}
});
MyWidget = WidgetFactory.extend({
init: function($el){
$el.html("My Widget!!")
}
});
Otherwise, the arguments to the
constructor are passed to init and the return value of setup
is discarded.
Deciding between setup
and init
Usually, you should use init to do your constructor function's initialization.
You should, instead, use setup
when:
- there is initialization code that you want to run before the inheriting constructor's
init
method is called. - there is initialization code that should run whether or not inheriting constructors
call their base's
init
methods. - you want to modify the arguments that will get passed to
init
.