相关疑难解决方法(0)

如何从异步调用返回响应?

我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo

我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });

    return result;
}

var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery asynchronous xmlhttprequest

5208
推荐指数
38
解决办法
134万
查看次数

"this"关键字如何运作?

我注意到,似乎没有明确解释this关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.

我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.

this工作如何以及何时使用?

javascript this

1243
推荐指数
16
解决办法
39万
查看次数

为什么我的变量在函数内部修改后没有改变? - 异步代码引用

鉴于以下示例,为什么outerScopeVar在所有情况下都未定义?

var outerScopeVar;

var img = document.createElement('img');
img.onload = function() {
    outerScopeVar = this.width;
};
img.src = 'lolcat.png';
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

var outerScopeVar;
setTimeout(function() {
    outerScopeVar = 'Hello Asynchronous World!';
}, 0);
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// Example using some jQuery
var outerScopeVar;
$.post('loldog', function(response) {
    outerScopeVar = response;
});
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// Node.js example
var outerScopeVar;
fs.readFile('./catdog.html', function(err, data) {
    outerScopeVar = data;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// with promises
var outerScopeVar;
myPromise.then(function (response) {
    outerScopeVar = response;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// geolocation API
var outerScopeVar; …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous

675
推荐指数
6
解决办法
18万
查看次数

React js onClick无法将值传递给方法

我想阅读onClick事件值属性.但是当我点击它时,我在控制台上看到类似的东西:

SyntheticMouseEvent {dispatchConfig: Object, dispatchMarker: ".1.1.0.2.0.0:1", nativeEvent: MouseEvent, type: "click", target
Run Code Online (Sandbox Code Playgroud)

我的代码工作正常.当我运行时,我可以看到{column}但无法在onClick事件中获取它.

我的代码:

var HeaderRows = React.createClass({
  handleSort:  function(value) {
    console.log(value);
  },
  render: function () {
    var that = this;
    return(
      <tr>
        {this.props.defaultColumns.map(function (column) {
          return (
            <th value={column} onClick={that.handleSort} >{column}</th>
          );
        })}
        {this.props.externalColumns.map(function (column) {
          // Multi dimension array - 0 is column name
          var externalColumnName = column[0];
          return ( <th>{externalColumnName}</th>);
        })}
      </tr>
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

如何将值传递给onClickReact js中的事件?

javascript reactjs

563
推荐指数
14
解决办法
46万
查看次数

这个JavaScript成语的基础是:var self = this?

我在WebKit HTML 5 SQL存储笔记演示的源代码中看到以下内容:

function Note() {
  var self = this;

  var note = document.createElement('div');
  note.className = 'note';
  note.addEventListener('mousedown', function(e) { return self.onMouseDown(e) }, false);
  note.addEventListener('click', function() { return self.onNoteClick() }, false);
  this.note = note;
  // ...
}
Run Code Online (Sandbox Code Playgroud)

笔者采用自我在一些地方(函数体)及在其他地方(的函数方法的参数列表中定义的机构).这是怎么回事?现在我已经注意到了它,我会在各处开始看到它吗?

javascript closures scope

353
推荐指数
8
解决办法
15万
查看次数

将正确的"this"上下文传递给setTimeout回调?

如何将上下文传递给setTimeout?我想打电话this.tip.destroy(),如果this.options.destroyOnHide在1000毫秒.我怎样才能做到这一点?

if (this.options.destroyOnHide) {
     setTimeout(function() { this.tip.destroy() }, 1000);
} 
Run Code Online (Sandbox Code Playgroud)

当我尝试以上时,this指的是窗口.

javascript callback this settimeout

226
推荐指数
4
解决办法
17万
查看次数

无法在事件处理程序中访问React实例(this)

我在ES6中编写了一个简单的组件(使用BabelJS),并且函数this.setState不起作用.

典型的错误包括

无法读取未定义的属性'setState'

要么

this.setState不是函数

你知道为什么吗?这是代码:

import React from 'react'

class SomeClass extends React.Component {
  constructor(props) {
    super(props)
    this.state = {inputContent: 'startValue'}
  }

  sendContent(e) {
    console.log('sending input content '+React.findDOMNode(React.refs.someref).value)
  }

  changeContent(e) {
    this.setState({inputContent: e.target.value})
  } 

  render() {
    return (
      <div>
        <h4>The input form is here:</h4>
        Title: 
        <input type="text" ref="someref" value={this.inputContent} 
          onChange={this.changeContent} /> 
        <button onClick={this.sendContent}>Submit</button>
      </div>
    )
  }
}

export default SomeClass
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 reactjs babeljs

201
推荐指数
4
解决办法
6万
查看次数

Angular 2 - 在setTimeout中使用'this'

我班上有这样的功能

  showMessageSuccess(){

    var that = this;
    this.messageSuccess = true;

    setTimeout(function(){
      that.messageSuccess = false;
    },3000);

  }
Run Code Online (Sandbox Code Playgroud)

我怎样才能重写这个,所以我不必在'that'var中存储对'this'的引用?如果我在setTimeout中使用'this',则messageSuccess bool似乎不会更改/获取更新.

javascript scope angular

110
推荐指数
1
解决办法
17万
查看次数

在地图函数Reactjs中,"this"未定义

我正在使用Reactjs编写一个菜单组件.

"use strict";

var React = require("react");
var Menus = React.createClass({

    item_url: function (item,categories,articles) {
        console.log('afdasfasfasdfasdf');
        var url='XXX';
        if (item.type == 1) {
            url = item.categoryId == null ? 'javascript:void(0)' : path('buex_portal_browse_category', {slug: categories[item.categoryId].slug});
        } else if (item.type == 2) {
            url = item.articleId == null ? 'javascript:void(0)' : path('buex_portal_view_article', {slug: articles[item.articleId].slug, id: item.articleId});
        } else {
            url = item.url;
        }
        return url;
    },

    render: function () {
     //   console.log(this.props.menus);  // return correctly
            var menuElements = this.props.menus.map(function (item1) { // …
Run Code Online (Sandbox Code Playgroud)

javascript this reactjs map-function

94
推荐指数
1
解决办法
4万
查看次数

如何使用ES6类扩展函数?

ES6允许扩展特殊对象.所以可以从函数继承.这样的对象可以作为函数调用,但是如何实现这种调用的逻辑呢?

class Smth extends Function {
  constructor (x) {
    // What should be done here
    super();
  }
}

(new Smth(256))() // to get 256 at this call?
Run Code Online (Sandbox Code Playgroud)

类的任何方法都可以通过引用类实例this.但是当它被称为函数时,this指的是window.当作为函数调用时,如何获取对类实例的引用?

PS:俄语同样的问题.

javascript inheritance function ecmascript-6 javascript-inheritance

93
推荐指数
4
解决办法
2万
查看次数