Javascript 的 rhino 动态属性

发布于 2024-09-30 16:41:50 字数 96 浏览 6 评论 0原文

在 Rhino 中,我可以通过在 Java 类上定义 js... 函数来添加特定属性。我想要的是定义一个包罗万象的函数,如果程序要引用尚未定义的属性,则该函数会被调用。有办法吗?

In Rhino, I can add specific properties by defining js... functions on a Java class. What I'd like is to define a catchall function that gets called if a program goes to reference a property that hasn't been otherwise defined. Is there a way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-10-07 16:41:50

我认为没有办法使用本机语法来表达这个概念,甚至使用 Rhino/Spidermonkey 专有扩展(如 getters 和 setters): https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Objects#Defining_Getters_and_Setters

但是,JavaScript 相当容易扩展,所以我认为你可以获得一些东西通过向 Object.prototype 添加一个方法来支持这种更高级样式的方法调用,效果非常接近。以下似乎可以满足您的要求:

Object.prototype.invokeMethodOrDefault = function(methodName,argsArray){
    if(this[methodName] && typeof this[methodName] == "function"){
        //invoke method with given arguments
        this[methodName].apply(this,argsArray)
    }else{
        this.defaultMethod.apply(this,argsArray)
    }
}

//add a defaultMethod noop that can be overridden for individual subclasses
Object.prototype.defaultMethod = function(){print("In default method")} 

//test it
foo = {
    helloWorld:function(){
        print("hello world!")
        print("args:")
        for(var i=0,l=arguments.length;i<l;i++){
            print(arguments[i]);
        } 
    }
}
foo.invokeMethodOrDefault("thisMethodDoesNotExist",[]) 

foo.invokeMethodOrDefault("helloWorld",["arg1","arg2"]) 


bar = {
    helloWorld2:function(){
        print("hello world2!")
        print("args:")
        for(var i=0,l=arguments.length;i<l;i++){
            print(arguments[i]);
        } 
    },
    defaultMethod:function(){
        print("in new default method")
    }
}

bar.invokeMethodOrDefault("thisMethodDoesNotExist",[]) 

bar.invokeMethodOrDefault("helloWorld2",["arg1","arg2"]) 

打印以下内容:

In default method
hello world!
args:
arg1
arg2
in new default method
hello world2!
args:
arg1
arg2   

I don't think there is a way to express this concept using native syntax, even using Rhino/Spidermonkey proprietary extensions like getters and setters: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Objects#Defining_Getters_and_Setters

However, JavaScript is fairly easy to extend, and so I think you could get something pretty close by adding a method to Object.prototype to support this more advanced style method invocation. The following would seem to do what you want:

Object.prototype.invokeMethodOrDefault = function(methodName,argsArray){
    if(this[methodName] && typeof this[methodName] == "function"){
        //invoke method with given arguments
        this[methodName].apply(this,argsArray)
    }else{
        this.defaultMethod.apply(this,argsArray)
    }
}

//add a defaultMethod noop that can be overridden for individual subclasses
Object.prototype.defaultMethod = function(){print("In default method")} 

//test it
foo = {
    helloWorld:function(){
        print("hello world!")
        print("args:")
        for(var i=0,l=arguments.length;i<l;i++){
            print(arguments[i]);
        } 
    }
}
foo.invokeMethodOrDefault("thisMethodDoesNotExist",[]) 

foo.invokeMethodOrDefault("helloWorld",["arg1","arg2"]) 


bar = {
    helloWorld2:function(){
        print("hello world2!")
        print("args:")
        for(var i=0,l=arguments.length;i<l;i++){
            print(arguments[i]);
        } 
    },
    defaultMethod:function(){
        print("in new default method")
    }
}

bar.invokeMethodOrDefault("thisMethodDoesNotExist",[]) 

bar.invokeMethodOrDefault("helloWorld2",["arg1","arg2"]) 

Which prints the following:

In default method
hello world!
args:
arg1
arg2
in new default method
hello world2!
args:
arg1
arg2   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文