JS对象自定义变量
我在某处读到,对象基本上是哈希表,您可以随意为它们分配值。好吧,我希望利用这一点,但我想知道它是否可能,是否被认为是“正确的”,以及是否存在任何不需要的情况。
我的情况:
我有很多对象(不能存储在 DOM 中的类型!),我想将它们分配给 DOM 对象。我的计划是:
找到一个dom对象(一个div,或某种形式的区域),然后将其分配给变量myVar,
然后我将调用:myVar.customVal = value customVal 当然没有在 DOM 规范中定义。但这行得通吗?它会显示在 DOM 中,还是保留为虚拟变量?有什么方法可以将自定义值分配给 DOM 成员以便稍后访问吗?
I read somewhere that objects were basically hash tables, and you could assign values to them willy nilly. Well, I am hoping to take advantage of this, but I want to know if it is even possible, if it is considered "correct", and, if there are any unwanted situations.
My situation:
I have a serious of objects (the kind which CANNOT be stored in the DOM!) which I want to assign to DOM objects. My plan is to:
FInd a dom object (A div, or area of some form), and then assign that to the variable myVar
I will then call: myVar.customVal = value
customVal of course is not defined in the DOM specification. Will that even work, though? Will it show up in the DOM, or stay a virtual variable? Is there any way to assign custom values to members of the DOM for access later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
但是不,这不被认为是好的做法。相反,请考虑使用 HTML5 的自定义数据属性,或者更好的是,jQuery 对它们的抽象。
You can do it:
But no, it's not considered good practice. Rather, consider using HTML5's custom data attributes, or better yet, jQuery's abstraction of them.