JSPatch 中 defineClass 中覆蓋方法的使用

來源:互聯網
上載者:User

標籤:

今天研究了一下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 中覆蓋方法的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.