Click to See Complete Forum and Search --> : Indexers
code?
June 13th, 2009, 07:20 PM
How can I define a custom object/class, but also for it to have an indexer, so that I can get values via something like this.
var value = group1["bob"]; //return a person class, for example
var planet = System[2] || System["Earth"]
It works much like this.
myForm["username"].value
olivthill2
June 14th, 2009, 06:50 AM
I presume it is Javascript. You can use an index instead of an ID, e.g.:
<html>
<head>
<script language="JavaScript">
function process_by_id()
{
document.getElementById("foo").value = "abc";
document.getElementById("bar").value = "def";
return true;
}
function process_by_index()
{
// alert(document.form1.elements.length);
document.form1.elements[0].value = "ABC";
document.form1.elements[1].value = "DEF";
return true;
}
</script>
</head>
<body>
<form id="form1" name="form1">
<input type="text" id="foo" value="foo">
<input type="text" id="bar" value="bar">
<input type="button" value="Change element by id" onClick="javascript:process_by_id();">
<input type="button" value="Change element by index" onClick="javascript:process_by_index();">
</form>
</body>
</html>
code?
June 15th, 2009, 12:05 AM
No, you don't understand what I want.
I want this.
var Groups =
{
_List: [],
get this() { return _List; }
};
If the javascript parser could take "get this() {}" as itself, rather then a property named "this", you could access it like this.
Groups["group1"] = ["bob", "mike", "tom"];
var group1 = Groups[0];
How can you have a custom variable/class that has an indexer?
Do I just have to make an object have array
PeejAvery
June 15th, 2009, 04:09 PM
I'm confused as to why you don't just use multidimensional arrays. Why can't your first dimension be indexed, and your second dimension be associative?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.