- 给JSContext对象添加属性
- 调用JSContext方法
# 给JSContext对象添加属性
JSContext *context = [[JSContext alloc] init];
JSValue *value = [context evaluateScript:@"1+1"];
NSLog(@"%@", value);
[context evaluateScript:@"var name = 'zhangsan'"];
NSLog(@"%@", context[@"name"]);
# 添加一个log方法
context[@"log"] = ^(){
//处理不定参数(调用时传的参数)
NSArray *array = [JSContext currentArguments];
for (id item in array) {
NSLog(@"%@", item);
}
};
# 调用方法
[context evaluateScript:@"log(1,2,3)"];```
例子:
JSContext *context = [[JSContext alloc] init];
context[@"dic"] = @{@"name":@"lala", @"age":@"11"};
context[@"log"] = ^(){
//处理不定参数
NSArray *array = [JSContext currentArguments];
for (id item in array) {
NSLog(@"%@", item);
}
};
[context evaluateScript:@"log(dic)"];
[context evaluateScript:@"log(dic.name)"];