#import <JavaScriptCore/JavaScriptCore.h>

 

之前一章已经看过JS和Block的互动

NSInteger nNumber = 9;
    NSString *strOut = @"abc";
    context[@"sum"] = ^(int a,int b)
    {
        NSLog(@"strOut = %@",strOut);
        return a+b+nNumber;
    };
    result = [context evaluateScript:@"sum(1,2);"];
    NSLog(@"sum2 = %f",[result toDouble]);

  打印结果

2015-01-28 16:23:24.453 JSBinding[5121:60b] strOut = abc

2015-01-28 16:23:24.454 JSBinding[5121:60b] sum2 = 12.000000


你会发现在Block中是调用,Block以外的变量从而满足你的一些特定需求。

如果你在Block中直接调用context是有问题的,如果我们要用

JSContext *ctx = [JSContext currentContext];

当然ctx是等同Block中的context的

 

相关文章:

  • 2021-09-02
  • 2022-03-10
  • 2022-12-23
  • 2022-01-30
  • 2021-07-10
  • 2022-12-23
  • 2021-07-01
猜你喜欢
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2021-12-04
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案