標籤:
今天研究了一下JSPatch,發現好神奇好奇妙,感覺這幾天我都會樂此不彼的去研究這個高大上的東西。
出處來著:https://github.com/bang590/JSPatch
簡單介紹一下這個 defineClass 方法,文檔中是這樣寫的(bang590/JSPatch )
defineClass(classDeclaration, instanceMethods, classMethods)
@param classDeclaration: 字串,類名/父類名和Protocol
@param instanceMethods: 要添加或覆蓋的執行個體方法
@param classMethods: 要添加或覆蓋的類方法
一、覆蓋方法
1.在 defineClass 裡定義 OC 已存在的方法即可覆蓋,方法名規則與調用規則一樣,使用 _ 分隔:
// OC@implementation JPTestObject- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"JSPath--:%d",indexPath.row);}@end// 注意 indexPath.row 在OC 和 JSPatch 中的調用方法不同,這一塊頭疼了我好久。// JSdefineClass("JPTableViewController", { tableView_didSelectRowAtIndexPath: function(tableView, indexPath) { ...
console.log("JSPath--:",indexPath.row()); // js 的輸出函數,可以在控制台輸出 },})
2.使用雙底線 __ 代表原OC方法名裡的底線 _ :
// OC@implementation JPTableViewController- (NSArray *) _dataSource {}@end// JSdefineClass("JPTableViewController", { __dataSource: function() { },})
3.在方法名前加 ORIG 即可調用未覆蓋前的 OC 原方法:
// OC@implementation JPTableViewController- (void) viewDidLoad {}@end// JSdefineClass("JPTableViewController", { viewDidLoad: function() { self.ORIGviewDidLoad(); },})
JSPatch 中 defineClass 中覆蓋方法的使用