开发者

JS object custom variables

开发者 https://www.devze.com 2023-03-06 10:02 出处:网络
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

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 se开发者_如何学Gorious 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?


You can do it:

var foo = document.getElementById('sidebar');
foo.party = 3;
console.dir(foo);

But no, it's not considered good practice. Rather, consider using HTML5's custom data attributes, or better yet, jQuery's abstraction of them.

0

精彩评论

暂无评论...
验证码 换一张
取 消