Bắt đầu với Đối tượng Java™ thuần túy cũ [POJO, trong ví dụ này] chứa tất cả logic nghiệp vụ cho tiện ích mở rộng của bạn
Ví dụ
public class Extension {
public static void log[String msg] {
System.out.println[msg];
}
}
Trong trường hợp này, POJO chứa một phương thức duy nhất. Tiện ích mở rộng điển hình của bạn chứa nhiều logic hơn. Ví dụ
static class FESIExtension implements JSExtension {
public void initializeExtension[JSGlobalObject go] throws JSException {
// Create the prototype
final JSObject prototype = go.makeJSObject[];
prototype.setMember["log", new JSFunctionAdapter[] {
public Object doCall[JSObject thisObject, Object[] args]
throws JSException {
if [args.length >= 1] {
Extension.log[args[0].toString[]];
}
return null;
}
}];
final JSObject obj = go.makeJSObject[prototype];
// This is the name of the object to be used in JavaScript Code
go.setMember["CustomExtension", obj];
go.setMember["log", new JSFunctionAdapter[] {
public Object doCall[JSObject thisObject, Object[] args]
throws JSException {
if [args.length >= 1] {
Extension.log[args[0].toString[]];
}
return null;
}
}];
go.setMember["Logger", new JSFunctionAdapter[] {
public Object doNew[JSObject thisObject, Object[] args]
throws JSException {
JSGlobalObject go = thisObject.getGlobalObject[];
JSObject proto = go.makeJSObject[];
proto.setMember["log", new JSFunctionAdapter[] {
public Object doCall[JSObject thisObject, Object[] args]
throws JSException {
if [args.length >= 1] {
Extension.log[args[0].toString[]];
}
return null;
}
}];
final JSObject obj = go.makeJSObject[proto];
return obj;
}
}];
}
}
Phần mở rộng FESI này có ba phần chính
- Đầu tiên, tiện ích mở rộng tạo một nguyên mẫu có tên
JSObject
và thêm phương thức “log” vào nguyên mẫu.final JSObject prototype = go.makeJSObject[]; prototype.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; go.setMember["CustomExtension", obj];
Nguyên mẫu
JSObject
sau đó được thêm vào
0 với tênstatic class FESIExtension implements JSExtension { public void initializeExtension[JSGlobalObject go] throws JSException { // Create the prototype final JSObject prototype = go.makeJSObject[]; prototype.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; final JSObject obj = go.makeJSObject[prototype]; // This is the name of the object to be used in JavaScript Code go.setMember["CustomExtension", obj]; go.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; go.setMember["Logger", new JSFunctionAdapter[] { public Object doNew[JSObject thisObject, Object[] args] throws JSException { JSGlobalObject go = thisObject.getGlobalObject[]; JSObject proto = go.makeJSObject[]; proto.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; final JSObject obj = go.makeJSObject[proto]; return obj; } }]; } }
1. Sự bổ sung này cho phép các tập lệnh gọistatic class FESIExtension implements JSExtension { public void initializeExtension[JSGlobalObject go] throws JSException { // Create the prototype final JSObject prototype = go.makeJSObject[]; prototype.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; final JSObject obj = go.makeJSObject[prototype]; // This is the name of the object to be used in JavaScript Code go.setMember["CustomExtension", obj]; go.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; go.setMember["Logger", new JSFunctionAdapter[] { public Object doNew[JSObject thisObject, Object[] args] throws JSException { JSGlobalObject go = thisObject.getGlobalObject[]; JSObject proto = go.makeJSObject[]; proto.setMember["log", new JSFunctionAdapter[] { public Object doCall[JSObject thisObject, Object[] args] throws JSException { if [args.length >= 1] { Extension.log[args[0].toString[]]; } return null; } }]; final JSObject obj = go.makeJSObject[proto]; return obj; } }]; } }