从包含函数

时间:2017-03-28 06:07:52

标签: javascript

我无法返回dataVariable,因此其他功能可以使用自动执行功能。 console.log现在正在吐出undefined ...知道我做错了什么吗?

(function(){

     var dataVariable;

      function boom(){
        dataVariable = document.querySelectorAll('.className'); 
        return dataVariable;
      }

      console.log(dataVariable);

})();

4 个答案:

答案 0 :(得分:1)

dataVariable在boom功能中分配,尝试执行boom和console log it =),希望这有帮助。

(function(){

  var dataVariable;

  function boom(){
    dataVariable = document.querySelectorAll('.className'); 
    return dataVariable;
  }

  console.log(boom());

})();

答案 1 :(得分:0)

内部函数将返回 @Override protected void onSaveInstanceState(Bundle outState) { outState.putString(STATE_URL, feedURL); outState.putInt(STATE_LIMIT, feedLimit); //save the rssFeed here outState.putString("RSS_FEED", mRssFeed); super.onSaveInstanceState(outState); } private class DownloadData extends AsyncTask<String, Void, String> { private static final String TAG = "DownloadData"; @Override protected void onPostExecute(String s) { super.onPostExecute(s); //Log.d(TAG, "onPostExecute: params are" + s); mRssFeed = s; ParseApplications parseApplications = new ParseApplications(); parseApplications.parse(mRssFeed); FeedAdapter feedAdapter = new FeedAdapter(MainActivity.this, R.layout.list_record, parseApplications.getApplications()); listApps.setAdapter(feedAdapter); } @Override protected String doInBackground(String... strings) { Log.d(TAG, "doInBackground: starts with " + strings[0]); String rssFeed = downloadXML(strings[0]); if (rssFeed == null) { Log.e(TAG, "doInBackground: Error Downloading"); } return rssFeed; } private String downloadXML(String urlPath) { StringBuilder xmlResult = new StringBuilder(); try { URL url = new URL(urlPath); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); int response = httpURLConnection.getResponseCode(); Log.d(TAG, "downloadXML: The response Code is " + response); BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); int charsRead; char[] inputBuffer = new char[5500]; while (true) { charsRead = reader.read(inputBuffer); if (charsRead < 0) break; if (charsRead > 0) xmlResult.append(String.copyValueOf(inputBuffer, 0, charsRead)); } reader.close(); return xmlResult.toString(); } catch (MalformedURLException ex) { Log.e(TAG, "downloadXML: Malformated URL Exp: " + ex.getMessage()); } catch (IOException ex) { Log.e(TAG, "downloadXML: IO Exp: " + ex.getMessage()); } return null; } } 。但外部功能并未设置为返回任何内容。你实际上也没有调用内部dataVariable函数,所以它还没有执行。

答案 2 :(得分:0)

除非你打电话,否则一个函数将声明将返回任何内容。    因此,var dataVariable ;结束时未初始化,因此其值undefined

  (function(){
    var dataVariable;

   function boom(){
    dataVariable = document.querySelectorAll('.className'); 
    return dataVariable;
  }
  boom();// missing line
  console.log(dataVariable);

})();

答案 3 :(得分:0)

您需要调用繁荣功能才能执行该代码。

function binarySearch(min, max, resizedElement, otherCoord, vertical=false) {

    function getElement(x, y) {
        if (vertical) {
            let tmp = x;
            x = y;
            y = tmp;
        }
        // we know there will always be an element touching, so this
        // should only throw an error if we pass bad otherCoord
        return document.elementsFromPoint(x, y).filter(e => e !== resizedElement)[0];
    }

    let elements    = new Set(),
        startIndex  = min,
        startElement= getElement(min, otherCoord),
        stopIndex   = max,
        stopElement = getElement(max, otherCoord);

    if (startElement === stopElement) {
        elements.add(startElement);
    } else {
        let middle = Math.floor((stopIndex + startIndex)/2),
            left   = binarySearch(min, middle, resizedElement, otherCoord, vertical),
            right  = binarySearch(middle, max, resizedElement, otherCoord, vertical);
        elements   = new Set([...elements, ...left, ...right]);
    }

    return elements;
}