我正在使用SAP UI5 Framework。我正在创建一个类的实例,然后更改类的构造函数。在创建类的第二个实例时,不会调用新的constrctor!
var myConstructor = function(){
alert('my own constructor');
}
var btn = new sap.m.Button({text:'Hello World'}) //sap.m.Button is a class
sap.m.Button.prototype.constructor = myConstructor; //Changing the constrcutor
var btn2 = new sap.m.Button({text:'h'}); // why myConstructor aint called !
以下是工作示例http://jsbin.com/voluc/2/edit
由于
答案 0 :(得分:2)
你实际上并没有通过这样做来改变构造函数
sap.m.Button.prototype.constructor = myConstructor;
您只是更改原型使用的属性以引用它的构造函数。
要完全更改构造函数,只需重新分配
即可sap.m.Button = myConstructor;
我仍然不知道你为什么这样做。如果要暂时更改构造函数,则可能需要先将其存储,然后再将其设置为初始版本。
var oldCtor = sap.m.Button;
sap.m.Button = = myConstructor;
// finished doing your tasks
sap.m.Button = oldCtor;
答案 1 :(得分:1)
更改默认构造函数会影响对象行为,所以不要这样做。
<script>
var myConstructor = function(){
alert('my own constructor');
}
var btn = new sap.m.Button({text:'Hello World'})
btn.placeAt('content');
sap.m.Button = myConstructor; //Changing the constrcutor
var btn2 = new sap.m.Button({text:'h'}); // why myConstructor ain't called !
console.log(sap.m.Button.constructor)
//but if you see i doh't write btn2.placeAt("content") because of your constructor don't do many things as need
</script>