是否有一种在香草JavaScript中聆听/代理变量的方法?
我正在构建一个网络框架,例如React;我想改善反应的一件事是状态。
我的想法是像Svelte这样的想法,使用状态您只需创建一个普通变量(在我的情况下,在创建TE状态时可以使用功能,但在更新时可以使用函数),但是Svelte是如何通过编译,来编译,我希望它可以在香草JavaScript中工作。
从我的理解来看,这是不可能的,但是我仍然试图以某种方式破解一些东西。
因此,该状态系统的一部分是不可能的,就是知道何时设置了原始的设置并获得(setters& getters),我希望它与范围的变量一起使用。因此,我无法在window> window
或globalthis this
上使用object.defineProperty
。我已经四处hack了一段时间,这是我认为唯一可以使用的解决方案:
- 代理一个
new String(String)
,给出了这种错误类型的蜜蜂的怪异错误,价值和东西。 - 代理
funtion.arguments
对象,但这无效。 - 使用
symbol.tprimitive
,但是我找不到没有+
或$ {}
的方法。
但是,如您所见,它们都有问题,我陷入困境,找不到任何东西,即使没有遗产或弃用的代码,即使是hacky,即使是hacky)?谢谢你!
I'm building a web framework, something like React; one of the things which I would like to improve on React is state.
My idea is something like Svelte, to use state you just create a normal variable (in my case it would be okay to use a function when creating te state, but not when updating it), but how Svelte does this Magic is by compiling, and I would like it to work in vanilla Javascript.
From my understanding this is not exactly possible, but I've still been trying to hack something somehow.
So the part of this state system that is not possible is knowing when a primitive is set and got (setters & getters), I want it to work with scoped variables; so I can't use the Object.defineProperty
on the window
or globalThis
. I've been hacking around for quite some time and here are the only solutions I thought have could worked:
- Proxing a
new String(string)
, has given weird error of this beeing of the wrong type, unknows values, and stuff. - Proxing the
Funtion.arguments
object, but this didn't work. - Using
Symbol.toPrimitive
, but I couldn't find a way of using it without a+
or${}
.
But as you can see they all have problems, I'm stuck and can't find anything, is there any (even if hacky, though without legacy or deprecated code) way to do this? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法做您在JavaScript中描述的事情。您不能代表原始的原始方式,并且只有在读取或设置a variable 时,只有在A property of时,就不能以其他方式运行代码(getter,setter)读取或设置对象。
在严格模式在其中有一个带有getters和setter的对象,然后将其放入用于使用语句的
使用
来解决自由主义标识符的环境中,但同样,它是不允许的严格模式下的充分理由(这是模块和其他创建新上下文的机制的默认原因,例如
class
的主体)。我不愿举一个例子,但是出于完整性:
您更新的问题:
我不会尝试使用独立变量来完成此操作,让用户提供状态对象并将其数据属性转换为
getter
/setter
组合(或用新的带有getter
/setter
组合等的版本:):You can't do what you've described in JavaScript. You can't proxy a primitive, and you can't run code some other way (getter, setter) when a variable is read or set, only when a property of an object is read or set.
There is an awful thing you can do in loose mode that's disallowed (for good reasons) in strict mode where you have an object with getters and setters that you then put into the environment used for resolving freestanding identifiers using the
with
statement, but again, it's disallowed for good reasons in strict mode (which is the default for modules and other mechanisms that create new contexts, like the body of aclass
).I hesitate to give an example of it, but for completeness:
Re your updated question:
I wouldn't try to do it with freestanding variables, have the user provide a state object and convert its data properties to
getter
/setter
combinations (or replace it with a new version withgetter
/setter
combinations, etc.):