It works by referencing the method with bracket or array notation. You can reference properties and methods of an object this way. For example: var obj = { test1: 'test_one', test2: 'test_two' }; alert( obj['test1'] ) // alerts test_one alert( obj.test1 ) // also alerts test_one
The other code doesn't work because your aren't looking for a property on an object. To make it work you evaluate the expression and then call the method like this. (expr ? 'a' : 'a')(); However, since your function 'a' is global it is a method of the window. You could use bracket notation to reference the method like this. window[ (expr ? 'a' : 'a') ]() -- Brandon Aaron On Sun, Jan 18, 2009 at 9:48 PM, Ami <aminad...@gmail.com> wrote: > > Thank you. > It's working :) > > Can you put a function name in an array ?! > > May you explain me WHY it's working? > $('div')[ (true? 'next' : 'before') ]().hide() > > I tried also this: > function a() {alert('Function a')} > [expr ? 'a' : 'a']() > but it's didn't work. why? > > On Jan 19, 5:15 am, Brandon Aaron <brandon.aa...@gmail.com> wrote: > > I believe you are looking for the following syntax: > > $(selector)[ (expr ? 'next' : 'before') ]().show(); > > > > -- > > Brandon Aaron > > > > On Sun, Jan 18, 2009 at 9:12 PM, Ami <aminad...@gmail.com> wrote: > > > > > Sorry about my grammar, English isn't my lang. > > > > > I am trying to write code like that: > > > var expr=true,selector='div'; > > > $(selector) (expr ? .next() : .before() ). show(); > > > > > But it's not JS syntax. So how can I do it? > > > > > I know,that I can do it like that: > > > if (expr) $(selector)..before(),show() else > > > $(selector).next().show() > > > > > But I am trying to find a solution, that return a jQuery object. > > > > > Thank you. >