Javascript 重写另一个函数中的变量

Javascript 重写另一个函数中的变量,javascript,git,Javascript,Git,我是否可以调用函数,然后在实际运行变量之前重写变量的内容 因此,我有一个函数,基本上可以像这样拉入我的Git配置文件: var GetGitInfo = function() { var xhr = new XMLHttpRequest(); var gitURL = "https://api.github.com/users/myself/repos"; xhr.open("GET", gitURL); xhr.send(null); xhr.onr

我是否可以调用函数,然后在实际运行变量之前重写变量的内容

因此,我有一个函数,基本上可以像这样拉入我的Git配置文件:

var GetGitInfo = function() {
    var xhr = new XMLHttpRequest();
    var gitURL = "https://api.github.com/users/myself/repos";

    xhr.open("GET", gitURL);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                // console.log(xhr.responseText);
                console.log(JSON.parse(xhr.responseText));
            } else {
                console.log('Error: ' + xhr.status);
            }
        }
    };
}
然后我在另一个步骤中通过执行
GetGitInfo()调用该函数一切正常

但是,如果我想调用函数并替换
gitURL
变量,我该如何实现呢

大概是

GetGitInfo(
  gotURL= "https://api.github.com/users/new_user/repo";
);
使用参数

var getData = function(url){
     // url can be used here
 }

var data = getData("http://apiurl.xy")

将参数传递给函数:

var GetGitInfo = function(gitURL) {
    var xhr = new XMLHttpRequest();

    xhr.open("GET", gitURL);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                // console.log(xhr.responseText);
                console.log(JSON.parse(xhr.responseText));
            } else {
                console.log('Error: ' + xhr.status);
            }
        }
    };
}
GetGetInfo("https://api.github.com/users/myself/repos");

不能从函数外部将局部变量修改为函数。它们对于函数的实现是私有的

但是,因为它是您自己的函数,所以您可以创建一个可以传递到函数中的参数。您甚至可以将参数设置为可选,以便在未传递时将初始值作为默认值

var GetGitInfo = function(url) {
    var xhr = new XMLHttpRequest();
    var gitURL = url || "https://api.github.com/users/myself/repos";

    xhr.open("GET", gitURL);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                // console.log(xhr.responseText);
                console.log(JSON.parse(xhr.responseText));
            } else {
                console.log('Error: ' + xhr.status);
            }
        }
    };
}
然后,您可以按照使用该函数的方式使用该函数,也可以传入URL以使用:

getGitInfo();                    // uses your default URL
getGitInfo("http://someURL");    // uses the URL you pass in

仅供参考,此函数看起来最终需要返回承诺或接受回调,以便将结果反馈给调用方。

从上面的代码片段中,您需要将url设置为函数参数,以便调用时使用指定的url

var GetInfo = function(url) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
        if (xhr.status === OK) {
            // console.log(xhr.responseText);
            console.log(JSON.parse(xhr.responseText));
        } else {
            console.log('Error: ' + xhr.status);
        }
    }
};

GetInfo("https://api.github.com/users/myself/repos");
您应该对函数执行toString():

var GetGitInfo = function(gitURL) {
    var xhr = new XMLHttpRequest();

    xhr.open("GET", gitURL);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                // console.log(xhr.responseText);
                console.log(JSON.parse(xhr.responseText));
            } else {
                console.log('Error: ' + xhr.status);
            }
        }
    };
}
GetGetInfo("https://api.github.com/users/myself/repos");
GetGitInfo.toString()

然后,您应该对变量及其数据进行文本搜索和替换:

GetGitInfo.toString().substring(0,GetGitInfo.indexOf('somestring'))+'gitUrl=“newURL”'+GetGitInfo.toString().substring(……)

那你应该评估那根绳子


或者,你知道,使用函数参数。不管怎样。最简单的方法。

只需在函数中添加一个参数:

var GetGitInfo = function(gitURL) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", gitURL);
    xhr.send(null);

    xhr.onreadystatechange = function() {
        var DONE = 4; // readyState 4 means the request is done.
        var OK = 200; // status 200 is a successful return.
        if (xhr.readyState === DONE) {
            if (xhr.status === OK) {
                // console.log(xhr.responseText);
                console.log(JSON.parse(xhr.responseText));
            } else {
                console.log('Error: ' + xhr.status);
            }
        }
   };
}

这样称呼它:

GetGitInfo("https://api.github.com/users/myself/repos");

比如说
GetGitInfo([gitURL=“new_url”])
@GerardoFurtadoOh,只需阅读下面的答案,忽略我的最后一点:
responseText
实际上将XML转换为字符串。这就是为什么我把它变成了一个JSON对象,这样我就可以对它进行迭代,从而得到一些逻辑。尽管如此,我仍在学习这一点,所以我可能是错的,不是吗;)