gpt4 book ai didi

rebol - 如何使用其字符串名称调用对象函数

转载 作者:行者123 更新时间:2023-12-02 07:43:00 24 4
gpt4 key购买 nike

知道以下为什么不起作用吗? (R3)

o: make object! [
foo: does [do "bar"]
bar: does [print "hello from bar"]
]

o/foo
** Script error: bar has no value
** Where: catch either -apply- do foo
** Near: catch/quit either var [[do/next data var]] [data]

最佳答案

试试这个:

o: make object! [
foo: does [do get bind load "bar" self]
bar: does [print "hello from bar"]
]
o/foo ;this will work

您需要绑定(bind),因为您的“栏”存在于对象中,而不是全局范围内。

也检查一下:

my-func: does [print "ok"]
o: make object! [
foo: does [do "my-func"]
bar: does [print "hello from bar"]
]
o/foo ;this will work too

您需要加载它,因为它是一个字符串,但它必须是一个单词才能被绑定(bind)。所以这些也可以工作(把它们放在你的对象中):

do get bind to-word "bar" self

do get bind 'bar self

关于rebol - 如何使用其字符串名称调用对象函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com