UI5 control inheritance implementation question

简介: UI5 control inheritance implementation question

C同事问了我这个问题。

使用下列这段代码extend UI5标准的控件。


image.png


问题1:为什么new 一个JerryButton时,line 35会触发?

var oJerryButton = new JerryButton({ application: oApplication } );


image.png


答案:

debug extend的实现, 在line 330里把调用extend指定的constructor赋给fnClass


image.png


然后在line 352把fnClass赋给fnClass.prototype.constructor. 这样,每次用fnClass这个构造器new新的instance时,constructor指向的function会自动被call到。


image.png

问题2

为什么把constructor的赋值放到extend function外面,由application自己显式赋值,但是在new的时候却不会被调用到了?


image.png


答案:

原因是此时传入的constructor为undefine,因此fnClass也是undefine


image.png


所以line 352注入的实际上是line 346动态生成的函数体为fnBaseClass.apply的function,这样当然我们自定义的constructor不会执行了。

如果你非要想在extend外面指定constructor,怎么弄?

只需要这样:

image.png


相关文章
UI5 control inheritance implementation question
UI5 control inheritance implementation question
UI5 control inheritance implementation question
Multiple Origin composition test - Opportunity Creation case
Sent: Wednesday, 3 December, 2014 2:48 PM 结论是:如果gateway系统上针对一个odata service维护了多个mark成default的backend system,在creation的case下,runtime时候gateway只会向第一个 Default system发起请求。
Multiple Origin composition test - Opportunity Creation case
A small tip to explore how to call a method of a control
Created by Jerry Wang, last modified on Mar 20, 2015
128 0
A small tip to explore how to call a method of a control
Fiori Elements - get annotation implementation in backend
Fiori Elements - get annotation implementation in backend
113 0
Fiori Elements - get annotation implementation in backend
How to create ABAP implicit enhancement implementation
How to create ABAP implicit enhancement implementation
131 0
How to create ABAP implicit enhancement implementation
how to know which settype an assignment block is built based on
how to know which settype an assignment block is built based on
how to know which settype an assignment block is built based on
ABAP Text table implementation
Created by Wang, Jerry, last modified on Dec 20, 2014
ABAP Text table implementation
SAP OData:MPC class get last modified - how to implement
Created by Wang, Jerry, last modified on Jan 06, 2015
SAP OData:MPC class get last modified - how to implement