I want to say:
var b = new a.B();
where a
is an object. I want the method B
to be able to refer to the calling object a
and to the newly created object b
.
Normally, first reference is achieved via method context and second - via constructor context. Both contexts bind referenced objects to this
keyword.
But how do I refer to both of them during a single call to B
?
USE CASE:
Suppose, that I've written a library that allows user to easily insert my widget inside his web-page. Upon load my library exports a single name to the global namespace: Toolbar
, which is a constructor that creates my widget within user's web-page element, called parent_dom_element
:
var toolbar1 = new Toolbar(parent_dom_element);
Now, within my widget, corresponding to toolbar1
object, user can create sub-widgets like this:
var comment = new toolbar1.Comment();
I want the constructor function Comment
to be able to refer to the toolbar1
object and to act as a constructor at the same time like this:
Toolbar.prototype.Comment = function (toolbar, ){
this.attr1 = toolbar.attr1;
};
How to achieve this?