I have an object in javascript:
admins: {
articles: {
path: '/admins/articles',
template: '/views/admins/articles.html',
link: function() {
return path; // !!! how to reference the 'path'?
}
}
}
I have a lot of objects like this, and each of them has a path field and a link function. I want to use the field path in link, but I can’t just use path.
What should I do?
You can use
thisto reference the object. Standardobject.method()“dot” syntax will setthistoobjectwithinmethod:Demo: http://jsfiddle.net/2Pt7n/
(There are other ways to call a function such that
thiswill not be set to the appropriate object, but I hope they don’t apply here – you don’t really say how you’re using the objects or calling the function, but if not in the way I showed then please update your question and I’ll update my answer accordingly.)