JSAppSugar 基于 JavaScript 语法定义的语法糖方法 项目简介
JSAppSugar 是基于 JavaScript 语法定义的一系列语法糖方法,包括:JavaScript 方法,Objective-C 方法和 Java 方法,以实现将开发 iOS/Mac/Android 原生应用程序的部分代码(一般来说是业务逻辑代码),用 JavaScript 代码来编写,从而简化开发过程,提高开发效率,以及提高程序质量。通过使用 JSAppSugar 的 Cocoa 和 Java 实现,开发者可以将原有原生代码的任何一部分改为 JavaScript 实现,并可以在 JavaScript 代码以 JavaScript 对象的使用语法使用由原生代码开发的类,而无需关心具体实现是由 Java 实现还是 Objective-C 实现。QuickStartUse JS Object In Objective-C//initialize JSA4Cocoa
JSA4Cocoa* jsa = [[JSA4Cocoa alloc]init];
[jsa startEngine];
//create a JS Object
id<JSAObject> jsObject = [jsa newClass:@"JSObject"];
//invoke methods on the object
NSString* text = [jsObject invokeMethod:@"getText"];Use JS Object In Java//initialize JSA4Java
JSA4Java jsa = new JSA4Java();
jsa.startEngine();
//create a JS Object
JSAObject jsObject = jsa.newClass("JSObject");
//invoke methods on the object
String text = (String)jsObject.invokeMethod("getText");Use Native Object In JavaScript//define the interface for native class
$class("NativeObject",{
$implementation:{
$java : "JavaObject",//the name of a java class
$oc : "OCObject"//the name of a OC class
},
getText:{
$java : "getText",
$oc : "getText:"
}
});
//define a js class. Then you can use this class in JavaScript, Java and objective-c
$class("JSObject",{
getText:function(){
//create a native object
var nativeObject = new NativeObject();
//invoke methods on the object
var text = nativeObject.getText("Hello");
return text;
}
});
JSA4Cocoa* jsa = [[JSA4Cocoa alloc]init];
[jsa startEngine];
//create a JS Object
id<JSAObject> jsObject = [jsa newClass:@"JSObject"];
//invoke methods on the object
NSString* text = [jsObject invokeMethod:@"getText"];Use JS Object In Java//initialize JSA4Java
JSA4Java jsa = new JSA4Java();
jsa.startEngine();
//create a JS Object
JSAObject jsObject = jsa.newClass("JSObject");
//invoke methods on the object
String text = (String)jsObject.invokeMethod("getText");Use Native Object In JavaScript//define the interface for native class
$class("NativeObject",{
$implementation:{
$java : "JavaObject",//the name of a java class
$oc : "OCObject"//the name of a OC class
},
getText:{
$java : "getText",
$oc : "getText:"
}
});
//define a js class. Then you can use this class in JavaScript, Java and objective-c
$class("JSObject",{
getText:function(){
//create a native object
var nativeObject = new NativeObject();
//invoke methods on the object
var text = nativeObject.getText("Hello");
return text;
}
});