javascript - 将函数作为参数传入js的疑问
巴扎黑
巴扎黑 2017-04-10 17:54:45
[JavaScript讨论组]

封装了一个ajax方法,想把回调函数作为封装好的参数传入,现在的做法是

function aa(m){
        m();
}
function bb(){
    alert(1);
}
aa(bb);

能运行,但总感觉不对,上网查了查,有这么做的,不过也有说是错的,所以来问问各位大神,小弟拜谢!

巴扎黑
巴扎黑

全部回复(1)
PHPz

代码如下,是这种逻辑

fn

function ajax(args) {
        $.ajax({
            type: args.type || 'post',
            url: args.url,
            data: args.data,
            async: args.async || true,
            dataType: args.datatype || 'json',
            success: function (m, status, xhr) {
                    args.success(m);
            },
            error: function(xhr, status, error) {
                console.log('xhr.status: ' + xhr.status + ' status: ' + status);
            }
        });
    }
    

调用

ajax({
  url: 'http://',
  data: {},
  success: function(j) {
      console.log(j);    //显示ajax返回的值
  }
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号