个人觉得学习iOS方面的知识,还得需要看苹果的开发文档,网上的教程还是比较片面,只能交给你怎么用,但是原理很少讲。这篇文章我主要讲的是文档里比较难理解的东西。 Demo地址:JavaScriptCoreTest
JSExport
The JSExport protocol provides a declarative way to export Objective-C instance classes and their instance methods, class methods, and properties to JavaScript code.
JSExport提供了一个声明的方式,来把OC的类,实例方法,类方法,属性这四方面暴露给JavaScript。这样一来JavaScript就可以调用OC的类方法或init方法创建实例,给属性赋值,调用实例的方法等。
Exporting Objective-C Objects to JavaScript
When you create a JavaScript value from an instance of an Objective-C class, and the JSValue class does not specify a copying convention, JavaScriptCore creates a JavaScript wrapper object. (For certain classes, JavaScriptCore automatically copies values to the appropriate JavaScript type; for example, NSString instances become JavaScript strings.)
JSValue是个什么东西呢?JSValue是对JavaScript事物的引用,我们知道JavaScript所有事物都是对象,那么JavaScriptCore就用JSValue来作为JavaScript value的标识。JSValue类可以用来转换OC与JavaScript的basic values(number, string等)来达到pass data的目的。比如说OC需要一个NSString对象,现在你知道了这个对象的JSValue值,那么可以调用- toString方法转换成NSString对象。如果你要传给JavaScriptCore一个JSValue,同理你调用[JSValue valueWithObject:inContext]可以将NSString对象a转换成JSValue value。
当你创建OC object的JSValue时,JSValue类并不能指定拷贝的约定,耳JavaScriptCore可以创建一个JavaScript包装对象来实现value的copy。
In JavaScript, inheritance is supported via a chain of prototype objects. For each Objective-C class you export, JavaScriptCore creates a prototype within the enclosing JavaScript context (a JSContext object). For the NSObject class, the prototype object is the JavaScript context's Object prototype. For all other Objective-C classes, JavaScriptCore creates a prototype object whose internal [Prototype] property points to the prototype property created for the Objective-C class's superclass. As such, the prototype chain for a JavaScript wrapper object reflects the wrapped Objective-C type's inheritance hierarchy.
JavaScript不是面向对象语言,而是基于对象语言。所以其就没有类,继承,多态,封装等属性。但是JavaScript可以使用a chain of prototype objects(原始对象响应链)的方式实现继承等。具体JavaScript如何实现自定义类和继承,可以上网查。我们的JavaScriptCore会为我们的NSObject或其他OC类创建一个prototype object来实现OC自定义类对象转换为JS自定义类对象。
Exposing Objective-C Methods and Properties to JavaScript
By default, no methods or properties of the Objective-C class are exposed to JavaScript; instead, you must choose methods and properties to export. For each protocol that a class conforms to, if the protocol incorporates the JSExport protocol, then JavaScriptCore interprets that protocol as a list of methods and properties to be exported to JavaScript.
For each instance method exported, JavaScriptCore creates a corresponding JavaScript function as a property of the prototype object. For each Objective-C property exported, JavaScriptCore creates a JavaScript accessor property on the prototype. For each class method exported, JavaScriptCore creates a JavaScript function on the constructor object. For example, Listing 1 and Listing 2 illustrate adoption of the JSExport protocol and the API presented by an exported class in JavaScript.
在JavaScript中,因为所有事物都是对象,所以function(函数)也是一个对象。在JavaScript code里有这样语句就不奇怪了:var calSum = function() {...};
For example, Listing 1 and Listing 2 illustrate adoption of the JSExport protocol and the API presented by an exported class in JavaScript.
Listing 1
Exporting an Objective-C Class to JavaScript
@protocol MyPointExports <JSExport>
@property double x;
@property double y;
- (NSString *)description;
- (instancetype)initWithX:(double)x y:(double)y;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
@interface MyPoint : NSObject <MyPointExports>
- (void)myPrivateMethod; // Not in the MyPointExports protocol, so not visible to JavaScript code.
@end
@implementation MyPoint
// ...
@end
Listing 2
Using an Exported Objective-C Class from JavaScript
// Objective-C properties become fields.
point.x;
point.x = 10;
// Objective-C instance methods become functions.
point.description();
// Objective-C initializers can be called with constructor syntax.
var p = MyPoint(1, 2);
// Objective-C class methods become functions on the constructor object.
var q = MyPoint.makePointWithXY(0, 0);
Bug:OC的initializers可以用constructor syntax.(构造函数)来实现,上面的代码var p = MyPoint(1, 2)确实也是这样。可是我自己在demo中用类似的方式根本不会创建实例p成功,后来网上查了下这样写才是正确的。给自己的initializers打了断点,确实是JS调用了。上面代码就var p = MyPoint(1, 2)有问题,其他类方法,属性赋值,实例方法调用是没问题的。
var p = new MyPoint(1, 2);