Javascript variable substitution eval

From richud.com
Jump to navigation Jump to search

Indirect Referencing / Double variable substitution in Javascript

Hopefully this will help someone get their head around this with eval in JS.

<SCRIPT>
onload=function(){

	interface="eth0"

	id="interface"
	alert(eval(id))

	id=interface
	alert(eval("id"))

	eth0 = "my lan"
	eval(interface+'='+interface+'+" it is now eth1"')
	alert(eth0)

	alert(eval(interface+'='+interface+'+" and eth2"'))

	alert(eval(interface+'='+interface+'+" and eth3"'))

}
</SCRIPT>

(If you end up having to do this you may be better off stuffing things into arrays / splitting strings to arrays etc. as you are probably looking at what you are trying to achieve wrongly)