简体   繁体   English

如何将JavaScript原型方法覆盖到我的普通JavaScript文件中?

[英]How override the JavaScript prototype method into my normal JavaScript file?

I have one method in JavaScript file which is written in prototype: 我在用原型编写的JavaScript文件中有一种方法:

SMRangePickerCtrl.prototype.rangeSelected = function (range) {      

    var self = this;
    self.onRangeSelect({range: range});
    self.value = { startDate: range.startDateAsMoment, endDate: range.endDateAsMoment, __$toString: range.text };  
};

Now, I want to inherit this method in the separate JavaScript file. 现在,我想在单独的JavaScript文件中继承此方法。

How can I do it? 我该怎么做?

Something like this? 像这样吗

var myMethod = SMRangePickerCtrl.prototype.rangeSelected;
myMethod(range);

If you need to add this method to some other object use the following: 如果需要将此方法添加到其他对象,请使用以下命令:

myObj.rangeSelected = SMRangePickerCtrl.prototype.rangeSelected.bind(myObj);

Or if you have an instance of SMRangePickerCtrl class say myAnotherObj and want to override this method use: 或者,如果您有SMRangePickerCtrl类的实例, SMRangePickerCtrlmyAnotherObj并想覆盖此方法,请使用:

myAnotherObj.rangeSelected = function(range) {
...
}

Or if you want to replace the method for all instances of SMRangePickerCtrl just use: 或者,如果要替换SMRangePickerCtrl所有实例的方法,请使用:

SMRangePickerCtrl.prototype.rangeSelected = function (range) {      
// My implementation here
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM