开发者

Jquery variables variable

开发者 https://www.devze.com 2023-01-16 03:39 出处:网络
There exist some concept like variables variable to print variable names or call f开发者_如何转开发unctions dynamically:

There exist some concept like variables variable to print variable names or call f开发者_如何转开发unctions dynamically:

http://php.net/manual/en/language.variables.variable.php

Thanks in advance.


The closest JavaScript equivalent is bracket notation, for example:

var obj = { myMethod: function() { alert("Hello!"); } };
var func = "myMethod";
obj[func](); //equal to obj.myMethod();

You can test it out here, in JavaScript calling these two is equivalent:

object.property
object["property"];

And the latter allows you to use a variable, to get any property or method you want.

To be clear this is a JavaScript behavior, there's nothing specific to jQuery about it.


In javascript you can use a similar aproach.

$a = "hello";
$['hello'] = 'world';
$[$a];
alert($a + " " + $[$a]); // alerts "hello world"

See in jsfiddle.

0

精彩评论

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