多个文件中的Javascript一个函数名 [英] Javascript one function name in multiple files

查看:58
本文介绍了多个文件中的Javascript一个函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所需要的很简单,尽管我不知道是否可能.

What I need is pretty simple, although I don't know if possible..

我将所有事件处理程序附加到对象本身,而不是附加到$(document).

I'm attaching all my event handlers to the object themselves instead of to $(document).

但是,我需要将它们附加到.ready()方法上,以确保在附加处理程序之前创建对象.由于要附加许多处理程序,为了使代码更整洁,我想创建一个名为attachHandlers()的函数,该函数将在document.ready()上调用.

But, I need to attach them on the .ready() method, to make sure the object is created before the handler is attached. Since there are a lot of handlers to attach, to keep my code cleaner I want to create a function called attachHandlers() that will be called on document.ready().

但是..此函数应该执行多个文件中的代码.

But.. this function should execute codes that are in multiple files.

我的问题是..是否可以多次声明相同的函数,然后只调用一次以执行所有函数?

My question is.. is there a way of declaring the same function multiple times, and then calling it just once to execute them all?

推荐答案

但是..此函数应该执行多个文件中的代码.

But.. this function should execute codes that are in multiple files.

我的问题是..有没有一种方法可以声明相同的功能 多次,然后只调用一次即可执行所有操作?

My question is.. is there a way of declaring the same function multiple times, and then calling it just once to execute them all?

如果正确解释问题,则要从.ready()处的多个文件中请求javascript.您可以创建一个包含要请求的文件的url的数组,使用 $.when() $.map() $.getScript() 来调用文件中的javascript

If interpret Question correctly, you want to request javascript from multiple files at .ready() . You can create an array containing url of file to request, use $.when(), $.map(), $.getScript() to call the javascript within the files

jQuery.getScript( url [, success ] )返回:jqXHR
描述: 使用GET HTTP请求从服务器加载JavaScript文件,然后 执行它.

jQuery.getScript( url [, success ] ) Returns: jqXHR
Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.

该脚本在全局上下文中执行,因此它可以引用其他脚本 变量并使用jQuery函数.包含的脚本可以有一些 对当前页面的影响.

The script is executed in the global context, so it can refer to other variables and use jQuery functions. Included scripts can have some impact on the current page.

$(document).ready(function(){

$(document).ready(function() {

  var files = ["1.js", "2.js", "3.js"];

  $.when.apply($, $.map(files, function(file) {
    // request file
    return $.getScript(file)
  }))
  .then(function() {
    // complete
    // do stuff
  }, function err(jqxhr, textStatus, errorThrown) {
    // handle error
  });

});

这篇关于多个文件中的Javascript一个函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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