我的目标是从其他函数 Submain
的原型向 Main 函数的(this)上下文添加属性function Main() {
Submain.call(this);
//from here I want to be able to use this.a, this.b, this.c, this.d
//...Main code...
}
function Submain() {
//this works fine
this.a = 1;
this.b = 2;
//this does not work because of the context
this.doThis();
}
Submain.prototype.doThis = function() {
this.c = 3;
this.d = 4;
}
那么,如何将 Submain 原型用于此目的?
答案 0 :(得分:0)
如果我理解正确,您缺少的部分是设置Main
构造函数的原型。人们通常会这样做:
Object.setPrototypeOf(Main.prototype, Submain.prototype);
然后你可以在构造函数中或从任何其他方法调用doThis
创建的对象上的new Main
:
var main = new Main();
console.log(main.c, main.d); // prints 3, 4
答案 1 :(得分:0)
我必须将 Submain 原型合并到 Main 原型。
可以使用 Object.assign (ES6)完成此操作。
<table border="0" cellpadding="0" cellspacing="0" width="95%" class="DataTable">
<tr>
<td width="30%" >
<div align="Center">
<a Href="" OnMouseOut="isimgact( 'btSelectWorkItm',0)" OnMouseOver="isimgact( 'btSelectWorkItm',1)" onclick ="return fnSubmitWorkList( document.template,'<%=workList.getNoOfChkBoxes()%>','selectWI','TWL','WorkList');">
<img src="<%=PAGEBUTTON%>/SelectWorkItm.gif" name= "btSelectWorkItm" alt="Select Work Item" tabindex="10" width="165" height="25" style="CURSOR: hand" border="0" ondrop="fnDrop()" ondragover="fnOverDrag()">
</a>
</div>
</td>
<td width="30%">
<div align="Center">
<a Href="" OnMouseOut="isimgact( 'btTnfToTeamUsr',0)" OnMouseOver="isimgact( 'btTnfToTeamUsr',1)" onclick ="return fnSubmitWorkList( document.template,'<%=workList.getNoOfChkBoxes()%>','transferTU','TWL','WorkItemRout');">
<img src="<%=PAGEBUTTON%>/TnfToTeamUsr.gif" name= "btTnfToTeamUsr" alt="Transfer To User" tabindex="10" width="165" height="25" style="CURSOR: hand" border="0" ondrop="fnDrop()" ondragover="fnOverDrag()">
</a>
</div>
</td>
<td width="30%">
<div align="Center">
<a Href="" OnMouseOut="isimgact( 'btTnfToSite',0)" OnMouseOver="isimgact( 'btTnfToSite',1)" onclick ="return fnSubmitWorkList( document.template,'<%=workList.getNoOfChkBoxes()%>','transferTS','TWL','WorkItemRout');">
<img src="<%=PAGEBUTTON%>/TnfToSite.gif" name= "btTnfToSite" alt="Transfer To Site" tabindex="10" width="165" height="25" style="CURSOR: hand" border="0" ondrop="fnDrop()" ondragover="fnOverDrag()">
</a>
</div>
</td>
</tr>
</table>