Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何定义此类函数?$。get(“路径”,函数(数据,状态){});_Javascript_Jquery - Fatal编程技术网

Javascript 如何定义此类函数?$。get(“路径”,函数(数据,状态){});

Javascript 如何定义此类函数?$。get(“路径”,函数(数据,状态){});,javascript,jquery,Javascript,Jquery,我需要一个例子。数据是如何从URL/path获取的,它是如何将数据作为参数传递到匿名函数的,代码是如何执行我们在匿名函数中编写的任何内容的 给出一个包含步骤的示例函数可以作为参数传递,接收函数可以在参数名称后使用()执行它。例如: function process(callback){ // do something - could be async or not, doesn't matter, then execute the callback, passing arguments

我需要一个例子。数据是如何从URL/path获取的,它是如何将数据作为参数传递到匿名函数的,代码是如何执行我们在匿名函数中编写的任何内容的


给出一个包含步骤的示例

函数可以作为参数传递,接收函数可以在参数名称后使用
()
执行它。例如:

function process(callback){
    // do something - could be async or not, doesn't matter, then execute the callback, passing arguments
    callback('something', 1, false);
}

process(function(firstArg, secondArg, thirdArg){
    console.log( "called" );
    console.log( firstArg );
    console.log( secondArg );
    console.log( thirdArg );
});

从URL获取数据是通过完成的,但不管函数在做什么,传递回调并执行回调的基本过程是相同的。

这称为回调函数。它作为参数传递给$.get函数,并从该函数内部使用特定参数调用。见下例:

function test(param1, callbackFunction) {
    if(param1) {
        var a = 1, b = 2;
        callbackFunction(a, b); // this is a call on the callbackFunction method received as parameter
    }
}

test(true, function(x, y) {
    console.log(x, y); // 1,2
});

为什么不阅读
$的jquery源代码。获取
?首先阅读关于xmlhttprequest的内容。$映射到JQuery。然后在jQuery对象的函数get中传递一个字符串“path”和一个函数,该函数依次接收两个参数data和status。假设取两个参数,比如var1,回调函数。将var1除以某个值并将其发送回回调函数。@bleeted0d不是作业。为了知识的目的。我是一名JavaScript开发人员,大部分时间我都在使用此类函数,例如在node js中,我们有类似的函数app.get('route',function(req,res){},我很想知道它的语法。是的,我真的很想知道。