模仿 PHP 的 __get()、__set() 和 __set() Node.js 中的 __call() 魔术方法

发布于 2024-10-10 08:13:32 字数 241 浏览 0 评论 0原文

我想知道是否有一种方法可以模仿 Node.js 中 PHP 的神奇方法 __get() 和 __set()。从这个问题: 适用于所有属性的 JavaScript getter 我知道你可以在 Rhino 中做到这一点,但 Node 是基于 V8 构建的。 V8有办法做到这一点吗?

I was wondering if there is a way to imitate PHP's magic methods __get() and __set() in Node. From this question: JavaScript getter for all properties I know you can do it in Rhino, but Node's built on V8. Does V8 have some way of doing this?

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

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

发布评论

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

评论(3

菩提树下叶撕阳。 2024-10-17 08:13:32

没有答案?看看nowjs。他们似乎找到了一种让 __get() 和 __set() 在 JavaScript 中工作的方法。我无法从源代码中弄清楚他们是如何做到的。 :(

编辑:查看监视器所有 JavaScript 对象属性(神奇的 getter 和 setter)

No answer? Check out nowjs. They seem to have found a way to make __get() and __set() work in JavaScript. I can't figure out how they do it from the source code. :(

EDIT: Check out Monitor All JavaScript Object Properties (magic getters and setters)

人事已非 2024-10-17 08:13:32

我相信您运气不好,至少截至 2010 年 3 月。至少你有 __defineGetter__ 和 __defineSetter__ ,尽管我意识到这不是一回事。一般来说,我认为使用 __noSuchMethod__/__get/method_missing 不好,因为它使代码更难阅读。考虑尝试不使用它,看看它是否会让你的代码更清晰。

I believe you're out of luck, at least as of March 2010. At least you have __defineGetter__ and __defineSetter__, though I realize that's not the same thing. In general I think using __noSuchMethod__/__get/method_missing is not good since it makes the code harder to read. Consider trying to get by without it and see if it makes your code clearer.

束缚m 2024-10-17 08:13:32

看看我的以下答案,它解释了如何使用ES6 代理

Have a look at my following answer that explains how one might tackle this with the use of ES6 proxies.

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