`
muyu
  • 浏览: 219028 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CALLBACK 与 FUNCTIONS

阅读更多

CALLBACK AND FUNCTIONS

--------------------------------
callback是一个函数(function),它作为另一个函数的参数,在其父级函数执行完毕之后才被执行。关于callback值得一提的是,位于父级之后的函数可以在callback执行前首先执行。

重要的是,如何正确地传递给callback。我过去经常忘记正确的语法而犯错。

 

没有参数的callback

--------------------
比如:

$.get('myhtmlpage.html', myCallBack);

 
注意:第二个参数仅仅是函数名(不要当做没有引号的字符串)。在JavaScript里,函数是‘第一型’,因此可以像变量引用来传递,并稍后执行。

 

带参数的callback

------------------------


错误的做法

$.get('myhtmlpage.html', myCallBack(param1, param2));

 
错误之处在于:
myCallBack(param1, param2)将被求值,然后当做$.get()的第二个参数。

 

正确的做法
上面例子的问题出在myCallBack(param1, param2)在被作为函数传递之前 被求值了。JavaScript和juqery扩展期望的是一个函数指针,比如setTimeout函数。

 

下面的用法,构造一个匿名函数(只是语句块),注册为callback函数。注意 'function(){'的用法。匿名函数“做了”一件事:调用带有 位于其他作用域的param1和param2值的myCallBack。

$.get('myhtmlpage.html', function(){
  myCallBack(param1, param2);
});

 
当$.get()获取到页面后,param1, param2才被求值,但仍然是callback。

 

原文地址

-----------

http://docs.jquery.com/How_jQuery_Works

分享到:
评论

相关推荐

    Callback-Functions:第6周

    回调函数第6周:回调函数一,文件清单回调函数和数组-Microsoft Word文档callbackFunctionExercise-JavaScript文件wordCountExercise-HTML文件sortingNumbersExercise-HTML文件二。 概念word文档定义了回调函数以及...

    Callback-Functions-in-[removed]此存储库包含 JavaScript 回调的演示

    JavaScript 中的回调函数几年前,我去了一家名为“The Inefficients”的餐厅。 跟上他们的名字,他们一直保持以下工作方式: 餐厅里有10张桌子,大约有6张桌子被占用。 首先,贝瑞先生来了,占据了 1 号桌,然后波莉...

    方维一元夺宝1.71商业版源码 支持PC端+WAP端+APP端+分销功能+多种支付方式+秒杀

    方维一元夺宝1.71商业版源码 支持PC端+WAP端+APP端+分销功能+多种支付方式+秒杀

    redux-saga-callback:redux-saga辅助函数等待调度的动作

    npm i redux-saga-callback 用法 import { putWait , withCallback } from 'redux-saga-callback' ; 用withCallback包装您的传奇发生器 takeEvery ( 'FETCH_USERS' , withCallback ( fetchUsers ) ) ; 如果需要,...

    igb_hwmon.rar_V2

    hwmon callback functions for Linux v2.13.6.

    H7 HAL驱动概要DM00392525_ENV2.pdf

    – Peripheral Init/DeInit HAL APIs can call user-callback functions to perform peripheral system level Initialization/De-Initialization (clock, GPIOs, interrupt, DMA) – Peripherals interrupt events ...

    jQuery.in.Action.3rd.Edition.161729207

    Chapter 9 Beyond the DOM with jQuery utility functions Chapter 10 Talk to the server with Ajax Chapter 11 Demo: an Ajax-powered contact form Part 3 Advanced topics Chapter 12 When jQuery is not ...

    PyGTK 2.0 Tutorial

    is implemented using the idea of classes and callback functions (pointers to functions). There is also a third component called GLib which contains a few replacements for some standard calls, as well ...

    acmcomponents16_delphi_audio_components_ACM_playback_

    New version of ACMComponents. A free component library that encapsulate windows ACM routines. Can record and playback with all acm codecs. Rewritten with callback functions

    matlab选择三维点坐标

    % other functions required: % CALLBACKCLICK3DPOINT mouse click callback function % ROWNORM returns norms of each row of a matrix % % To test this function ... % pointCloud = rand(3,100)*100; % h = ...

    [精典材料]Patran的PCL用户手册.doc

    Callback Arguments (Widget Function Data) 82 Widget Placement 84 Widget Variables 86 Modifying Widgets 87 Getting Data from Widgets 88 Putting it all Together (Example) 89 Exercise 8: Simple Form 91 ...

    DynamicWrapperX

    Added callback, which enables API functions to call script functions. This is necessary for using APIs such as EnumWindows (see examples below). On Windows 98, excluded the necessity to create a ...

    matlab GUI仿真.docx

    通过设置控件属性和回调函数(callback functions),用户可以定义每个元素的功能和响应逻辑,实现数据的输入、处理及结果显示。 例如,在一个控制系统仿真项目中,用户可以通过GUI设计一个界面,其中包含输入参数的...

    react-basket:一个基于material-ui组件的React的购物篮组件库

    React篮 一个基于material-ui组件的React的购物篮组件库。 主要特征 购物车组件 实时购物车支持 在应用程序中的任何位置连接到购物车数据 演示版 您可以从访问演示页面。... // You can call callback functions

    函数即为服务平台Picasso.zip

    Picasso = OpenStack API for Functions as a Service Picasso 可以让你在 OpenStack 平台上实现函数即为服务的功能。为包括开发人员和运维人员提供一个简单高效可伸缩的基础架构层。Picasso API 服务器使用 ...

    ftplib-4.0

    FtpSetCallback() - Establish a callback function FtpClearCallback() - Remove a callback function Server Connection FtpConnect() - Connect to a remote server FtpLogin() - Login to remote machine ...

    centos中的php.ini文件

    unserialize_callback_func = serialize_precision = 100 allow_call_time_pass_reference = Off safe_mode = Off safe_mode_gid = Off safe_mode_include_dir = safe_mode_exec_dir = safe_mode_allowed_env_vars =...

    node-promise.zip

    execute() - Executes a function that takes a callback and returns a promise (thank you Benjamin Thomas for providing this) 示例代码: var Promise = require("promise").Promise; var promise = new ...

    HTML Editor

    Subclass an ImageCombo control to create an owner-drawn combobox that functions like Excel's font preview ComboBox. Also includes a callback function to enumerate available fonts.

    matpower的最新版本出来了401-matpower4.0b1.zip

     - New functions:  - scale_load() conveniently modifies multiple loads.  - makeLODF() computes line outage distribution factors.  - total_load() retreives total load for the entire system, a ...

Global site tag (gtag.js) - Google Analytics