-
June 13th, 2009, 06:20 PM
#1
Indexers
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.
Code:
var value = group1["bob"]; //return a person class, for example
var planet = System[2] || System["Earth"]
It works much like this.
Code:
myForm["username"].value
-
June 14th, 2009, 05:50 AM
#2
Re: Indexers
I presume it is Javascript. You can use an index instead of an ID, e.g.:
Code:
<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>
-
June 14th, 2009, 11:05 PM
#3
Re: Indexers
No, you don't understand what I want.
I want this.
Code:
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.
Code:
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
Last edited by code?; June 15th, 2009 at 02:19 PM.
-
June 15th, 2009, 03:09 PM
#4
Re: Indexers
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?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|