javascript - pass the context on to an inner function -
here code:
function link(scope, element, attrs, formctrl) { element.tooltipster({ content: '', theme: 'validation-notify', arrowcolor: '#d9534f', contentashtml: true, functionbefore: generatecontent }); } function generatecontent() { var formfield = formctrl[element]; var = 0; }
the above code broken because formctrl , element not available in generatecontent
. now, realize use:
functionbefore: generatecontent.bind({formctrl: formctrl, element: element})
and in generatecontent
reference them by
this.element
i declare generatecontent inside of link. there better way of doing this? there anyway of binding generatecontent entire context within link, like:
functionbefore: generatecontent.bind(this)
(except in case this
undefined)?
like there anyway of binding generatecontent entire context within link
nope, functions lexically scoped , remember environment in created. cannot access information, implementation specific.
but can is, make generatecontent
function accept parameters needs , can define functionbefore
, this
... functionbefore: generatecontent.bind(this, formctrl, element) ... function generatecontent(formctrl, element) { var formfield = formctrl[element]; var = 0; }
here, generatecontent.bind(this, formctrl, element)
creates new function object when invoked, have context link
function's this
, 2 parameters formctrl
, element
default.
Comments
Post a Comment