foo(),foo和function(){foo()}之间的区别js [英] js difference between foo(), foo and function(){foo()}

查看:199
本文介绍了foo(),foo和function(){foo()}之间的区别js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在设置对话框后,我有以下代码位:

  $('#bar')。click(foo('baz')); 

当我加载页面并使对话框出现时,运行foo(不按#bar)。

如果代码是:

  $('#bar' )。单击(FOO); 

 <$ 。C $ C> $( '#巴')上单击(函数(){FOO( '巴兹')}); 

foo不会运行。

这是为什么?
指示我应该在哪里RTFM,感激地收到。



澄清:为什么foo在没有按下按钮的情况下运行

哦,我想我只是把它搞清楚了。



1.在foo的结果被传递给点击。
在2.我仍然不确定。
In 3. anon函数被传递给click。

解决方案

这不会等待点击调用 foo

  $('#bar')。click FOO( '巴兹')); 

foo('baz')是在执行该行时执行,而不是当用户单击 #bar 时)



这不会传递参数到 foo

  $('#bar')。click FOO); 

这可能是您需要的(除非 foo 构建并返回一个函数,我猜它不会):

$ $ $ $'''函数(){FOO( '巴兹')});

它不起作用的原因可能是您的元素id bar
$ b

详细解释:没有找到,或者您没有点击它。

p>

当您执行

  foo(bar); 

使用参数<$ c执行 foo $ c> bar



您执行的文件

  a = foo; 

你给变量 a foo ,这可能是一个函数或不是。



当你打电话给

  var a = function(){foo (酒吧)}; 

创建一个新的匿名函数,它执行 foo(bar) code>被调用时,您将此新函数作为变量的值 a



当你执行

  $('#bar')。click(function(){foo('baz')}); 

你传递一个新函数,调用 foo('baz')当被调用时,作为 $('#bar')的参数。click(。当你点击id <$ c $的元素时,JQuery调用这个函数c> bar


I have a jquery dialog with a button on it (my button, not one of the dialog buttons).

After the dialog is set up I have the following bit of code:

$('#bar').click(foo('baz'));

When I load the page and make the dialog appear, foo is run(without pressing #bar).

If the code is:

$('#bar').click(foo);

or

$('#bar').click(function(){foo('baz')});

foo doesn't run.

Why is that? Pointers to where I should RTFM, gratefully received.

Just to clarify: Why does foo run in the first instance without the button being pressed (ie upon dialog initialisation) and foo not run in the latter 2 examples.

Oh, I think I just worked it out.

In 1. the result of foo is being passed to click. In 2. I'm still not sure. In 3. The anon function is being passed to click.

解决方案

This doesn't wait for the click to call foo :

$('#bar').click(foo('baz'));

(foo('baz') is executed when this line is executed, not when the user click #bar)

This doesn't pass the argument to foo :

$('#bar').click(foo);

This is probably what you need (unless foo builds and returns a function, which I guess it doesn't) :

$('#bar').click(function(){foo('baz')});

The fact it doesn't work may be that your element with id bar isn't found or you don't click it.


Detailed explanation :

When you execute

foo(bar); 

you execute foo with argument bar.

Wen you execute

a = foo;

you give to the variable a the value foo, which may be a function or not.

When you call

var a = function() {foo(bar)};

you create a new anonymous function, which executes foo(bar) when called, and you gives this new function as value of the variable a.

When you execute

$('#bar').click(function(){foo('baz')});

you pass a new function, calling foo('baz') when called, as argument to $('#bar').click(. JQuery calls this function when you click the element with id bar.

这篇关于foo(),foo和function(){foo()}之间的区别js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆