在JavaScript中调用带有括号和不带有括号的函数的区别

编程入门 行业动态 更新时间:2024-10-25 14:34:33
本文介绍了在JavaScript中调用带有括号和不带有括号的函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理一个JavaScript文件上传事件.而且我有以下初始化程序和以下函数:

I was handling a JavaScript file upload event. And I have the following initializer and the following function:

初始化器

$('#s3-uploader').S3Uploader({ allow_multiple_files: false, before_add: progressBar.show, progress_bar_target: $('.upload-progress-bar'), remove_completed_progress_bar: false }).bind("s3_upload_complete", function(e, content) { console.log(content); });

功能

var progressBar = { show: function() { $('.upload-progress-bar').show(); return true; } }

在初始化程序中,我发现如果这样做,则有所不同

In the initializer, I noticed there is a difference if I do

before_add:progressBar.show 版本 before_add:progressBar.show().有括号的话,即使绑定到 before_add 选项,它也会被调用一次,而没有括号的话,它将不会被调用.

before_add: progressBar.show v.s. before_add: progressBar.show(). With the parentheses, it will be called once even if it is bound to the before_add option, and without the parentheses it will not.

我观察到的行为是否有解释?

Is there an explanation for the behaviour I observed?

推荐答案

使用括号,因为括号和结果被调用该调用的em>将存储在before_add中.

With parentheses the method is invoked because of the parentheses, and the result of that invocation will be stored in before_add.

不包含的括号中,您存储了对该变量中函数的引用(如果可以,则为指针").这样,只要有人调用before_add(),它就会被调用.

Without the parentheses you store a reference (or "pointer" if you will) to the function in the variable. That way it will be invoked whenever someone invokes before_add().

如果这不能解决问题,也许这会有所帮助:

If that didn't clear things up, maybe this will help:

function Foo() { return 'Cool!'; } function Bar(arg) { console.log(arg); } // Store the >>result of the invocation of the Foo function<< into X var x = Foo(); console.log(x); // Store >>a reference to the Bar function<< in y var y = Bar; // Invoke the referenced method y('Woah!'); // Also, show what y is: console.log(y); // Now, try Bar **with** parentheses: var z = Bar('Whut?'); // By now, 'Whut?' as already been output to the console; the below line will // return undefined because the invocation of Bar() didn't return anything. console.log(z);

如果您随后查看浏览器的控制台窗口,应该会看到:

If you then take a look at your browsers' console window you should see:

Cool! Woah! function Bar(arg) Whut? undefined

第1行是调用 Foo(),的结果第2行是调用 Bar() 通过" y ,第3行是 y ,的内容"第4行是 var z = Bar('Whut?'); 行的结果;Bar功能被调用,第5行显示调用 Bar()并将结果分配给 z 不会返回任何内容(因此: undefined ).

Line 1 is the result of invoking Foo(), Line 2 is the result of invoking Bar() "via" y, Line 3 is the "contents" of y, Line 4 is the result of the var z = Bar('Whut?'); line; the Bar function is invoked, Line 5 shows that invoking Bar() and assigning the result to z didn't return anything (thus: undefined).

更多推荐

在JavaScript中调用带有括号和不带有括号的函数的区别

本文发布于:2023-07-28 14:07:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   函数   区别   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!