java rhino 例子_使用rhino的JavaAdapter的示例

取决于

你想继承。我发现如果我使用JavaScript原型来定义对象,那么我只能得到Java对象的静态方法:

function test() {

this.hello = function() {

for(var i in this) {

println(i);

}

};

}

test.prototype= com.acme.app.TestClass; // use your class with static methods

// see the inheritance in action:

var testInstance=new test();

test.hello();

但是,JavaScript也允许对对象实例进行原型分配,因此可以使用这样的函数,并获得更类似Java的继承行为:

function test(baseInstance) {

var obj = new function() {

this.hello=function() {

for(var i in this) {

println(i);

}

};

};

obj.prototype=baseInstance; // inherit from baseInstance.

}

// see the thing in action:

var testInstance=test(new com.acme.TestClass()); // provide your base type

testInstance.hello();

或者使用一个函数(例如。

init

)类似于对象本身的上述函数:

function test() {

this.init=function(baseInstance) {

this.prototype=baseInstance;

};

this.hello=function() {

for(var i in this) {

println(i);

}

};

}

var testInstance=new test();

println(typeof(testInstance.prototype)); // is null or undefined

testInstance.init(new com.acme.TestClass()); // inherit from base object

// see it in action:

println(typeof(testInstance.prototype)); // is now com.acme.TestClass

testInstance.hello();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值