JavaScript 相当于 PHP 的 __get() 魔法方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3959187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:49:46  来源:igfitidea点击:

JavaScript's equivalent to PHP's __get() magic method

phpjavascriptpropertiesgetundefined

提问by Michal Pietrzyk

Possible Duplicate:
JavaScript getter for all properties

可能的重复:
所有属性的 JavaScript getter

Does JavaScript provide a method to access undefined object properties? In PHP the solution is to declare and implement __get()method in class.
Solutions using try { .. } catch { .. }are not sufficient for me, because I already have very large amount of code which actually needs to stay as-is.

JavaScript 是否提供访问未定义对象属性的方法?在 PHP 中,解决方案是__get()在类中声明和实现方法。
使用try { .. } catch { .. }的解决方案对我来说还不够,因为我已经有大量的代码实际上需要保持原样。

回答by Elzo Valugi

You may choose to write a similar functionality and check existing parameters them against the argumentsarray. I'm not 100% that this will solve your problem.

您可以选择编写类似的功能并根据参数数组检查现有参数。我不是 100% 这会解决你的问题。