我试图在 SAPUI5 应用程序中实现 Component.js,但无法理解下面代码中的.extend 和 .prototype.init.apply 方法。
sap.ui.define([
"sap/ui/core/UIComponent"
], function (UIComponent) {
"use strict";
return UIComponent.extend(""** , {**
init: function () {
UIComponent.prototype.init.apply(this, arguments);
// console.log(UIComponent.extend);
UIComponent.prototype.init.apply(this, arguments);
}
});
});
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?
PS 我是 OO Javascript 的初学者。
他们在这里所做的事情非常Java 风格。他们正在extend创建UIComponent.
在此子类中,该init方法被重写。当您重写父对象的方法时,最好从重写父对象的方法中调用父对象的原始方法。通过这样做,您可以避免意外情况,例如尚未在父级定义的变量等。调用父级的原始方法正是该init.apply语句正在执行的操作。不过,这样做两次对我来说没有意义。