2016-08-12 50 views
-3

我可以调用一个函数,然后在实际运行之前覆盖变量的内容吗?覆盖另一个函数中的变量JS

所以我基本上在拉我的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"; 
); 
+3

使用的参数。 –

+0

可能回调? – Script47

+0

所以像'GetGitInfo([gitURL =“new_url”]);'@GerardoFurtado – PourMeSomeCode

回答

1

您不能修改局部变量的函数功能之外。它们对函数的实现是私有的。

但是,由于它是您自己的功能,您可以创建一个可以传递给函数的参数。你甚至可以将参数设置为可选参数,这样如果它没有通过,它会将你的初始值作为默认值。

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 

FYI,这个功能看起来像它最终将需要或者回报承诺或接受回调,以便您可以将结果传回给调用者。

0

使用参数

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

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

参数传递给函数:

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"); 
1

从上面的代码片段中,您需要将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"); 
1

你应该做一个toString()的函数:

GetGitInfo.toString()

那么你应该做一个文本搜索和变量替换,它的数据:

GetGitInfo.toString().substring(0,GetGitInfo.indexOf('somestring'))+'gitUrl="newURL"'+GetGitInfo.toString().substring(.......)

然后,你应该评估该字符串!

或者,你知道,使用函数参数。无论哪种方式。无论什么最简单。

+0

从我读到的,'responseText'实际上采用XML并转它变成一个字符串。这就是为什么我把它变成一个JSON对象,所以我可以迭代它,所以一些逻辑。尽管如此,我仍然在学习这一点,所以我可能是错的,不太相信我);) – PourMeSomeCode

0

只是一个参数添加到您的函数:

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"); 
相关问题