标签: this

在点击事件函数中使用 this(类对象)

我在按钮上有一个单击事件,我必须传递此类对象才能调用单击事件内的函数。我的要求如下

$(this).on('click', function(){
     this.callFunc();
});
Run Code Online (Sandbox Code Playgroud)

现在,我将其存储在 self 中并在单击函数中使用它。目前我有以下代码来实现此目的。

var self = this;
$(this).on('click', function(){
     self.callFunc();
});
Run Code Online (Sandbox Code Playgroud)

有没有办法直接使用这个里面的点击功能?

javascript jquery this dom-events

0
推荐指数
1
解决办法
938
查看次数

在 Javascript 函数中调用 Typescript 函数

Uncaught TypeError: Object #<Object> has no method 'getInvoices' 当我调用this.getInvoicesajax.error 结果时出现错误。如何从那里访问打字稿功能?

// Typescript
class InvoicesController {
     ...

     public getInvoices(skip: number, take: number): void {
         ...    
     }

     public createInvoice() {
          $.ajax({
            ...
            contentType: 'application/json',
            type: 'POST',
            success: function (res) {
                if (res.result === 'ok') {
                    this.getInvoices(0,100); // THIS DOES NOT WORK? 
                }
            },
            error: function (err) {
                this.getInvoices(0,100); // THIS DOES NOT WORK?
            }
        });
     }
}
Run Code Online (Sandbox Code Playgroud)

javascript this typescript

0
推荐指数
1
解决办法
4983
查看次数

为什么使用设置为这个对象的数组调用 array.prototype.forEach.call() 不起作用

这里我有一个数字数组。我试图测试是否Array.prototype.forEach可以以与传统方式不同的方式在阵列上使用。在传统方式中,我们将 THIS 参数作为第二个参数传递给forEach.
在这里我使用Array.prototype.forEach.call(),为此我使用数组作为调用方法的参数。
但这表明window对象。
这是为什么 ?

number=[1,2,3,4,5];

Array.prototype.forEach.call(number,function(elem){
    console.log(this);
});
Run Code Online (Sandbox Code Playgroud)

javascript arrays foreach this

0
推荐指数
1
解决办法
8665
查看次数

`this` 的属性在 setTimeout 中未定义

class Simulator {
  constructor() {
    this.gates = Array();
    this.gates.push(new AndGate(200, 200));
  }
  initialize() {
    let canvas = document.getElementById('board');
    canvas.width = 800;
    canvas.height = 500;
    canvas.setAttribute("style", "border: 1px solid black");
    this.gates.push(new AndGate(100, 100));
  }
  run() {
    setTimeout(this.onLoop, 1000);
  }
  onLoop() {
    for (let gate of this.gates) {
      gate.render();
    }
  }
}
let sim = new Simulator();
sim.initialize();
sim.run();
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我的 TypeScript 类的 JS 转译版本在onLoop函数中引发错误。它报告TypeError: this.gates is undefined。但是,如果我访问sim(一个 Simulator 对象)并手动访问它定义的 gates 属性。我可以从控制台手动运行 onLoop 代码。

javascript this

0
推荐指数
1
解决办法
660
查看次数

this.setState 不是函数,react-native

它告诉我 this.setState' 是未定义的,但我不确定我能做些什么来使“this”指向 setState 的正确上下文。我不知道我还能绑定什么来帮助。

 import React from 'react';
 import { StyleSheet, Text, View } from 'react-native';
 import axios from 'react-native-axios';

 export default class App extends React.Component {
   constructor(props) {
   super(props);
   this.state = {
     data: 50
   }
   this.apirequest = this.apirequest.bind(this);
 }

  componentWillMount() {
    this.apirequest();
  }

  apirequest() { 

    axios.get('http://api.openweathermap.org/data/2.5/weather')
    .then(function (response) {
      console.log(response.data.main.temp);
      this.setState({
         data: response.data.main.temp 
       })
    })
   .catch(function (error) {
     console.log(error);
    });
}
Run Code Online (Sandbox Code Playgroud)

this reactjs react-native

0
推荐指数
1
解决办法
3329
查看次数

这个()在javascript中是什么?

我理解thisjavascript中的关键字.我已经用它喜欢this.method()this.variable =.但这是什么().请参阅以下代码:

  static fromTX(tx, index) {
    return new this().fromTX(tx, index);
  }
Run Code Online (Sandbox Code Playgroud)

请帮助我理解在javascript和上面的代码示例中使用this().

javascript this

0
推荐指数
1
解决办法
152
查看次数

对类的每个类变量使用“this”关键字是一种好习惯吗?

我只想知道在课堂上将这个关键字与类变量一起使用是好的做法还是坏的做法?

如果我使用会减少或增加应用程序的运行时间/编译时间吗?如下所示:-

this.FirsName = cust.name;
this.LastName = cust.Lname;
this.Age = cust.age;
this.DOB = cust.dob;
Run Code Online (Sandbox Code Playgroud)

等等超过 1000 行代码......

.net c# this

0
推荐指数
1
解决办法
97
查看次数

在成员函数中保留对对象本身的引用

在节省时间和减少操作数量方面,(*this)this->经常使用of的成员函数的开头创建对它的引用有用吗?编译器(gcc最让我感兴趣)已经为我优化了吗?有没有理由不这样做?

例:

void A::checkBytes( const byte * dataChunk, uint32_t chunkSize )
{
    A & self = (*this);
    bool UTF8Valid = self.InvalidSequences & 1;
    byte current, expectedUTF8Bytes = 0;
    for (uint32_t i = 0; i < chunkSize; i++)
    {
        current = dataChunk[i];

        // many tests with 'current' and 'this->InvalidSequences'

        self.Count[current]++;
        self.ChunkSize++;
    }
    if (!UTF8Valid) self.InvalidSequences |= 1;
}
Run Code Online (Sandbox Code Playgroud)

我知道每个非静态成员函数都有自己的hidden this。我知道我会同时拥有隐藏的A * this和隐藏的A & self。我不知道很多东西是否this->someMember会比很多东西花费更多referenceToThis.someMember

c++ optimization reference this

0
推荐指数
1
解决办法
54
查看次数

C++ `this` pointer

1) How this pointer is different from other pointers? As I understand pointers point to the memory in heap. Does that mean objects are always constructed in heap, given that there is pointer to them?

2)Can we steal this pointer in move constructor or move assignment?

c++ move this c++14

0
推荐指数
2
解决办法
231
查看次数

为什么叫布尔铸造?

为什么bool要调用演员表?

Set result(*this)调用构造函数时出现问题。我希望它使用拷贝构造函数,而不是它转换*thisbool并使用它作为一个int的构造函数。

如何修复它以使用复制构造函数?

Set Set::operator+(const Set& rhs)const
{
    Set result(*this);

    for (unsigned int i = 0; i < rhs.getSize(); i++)
    {
        result.add(rhs[i]);
    }
    return result;
}

Set::operator bool()const
{
    return !!(*this);
}

Set::Set(size_t capacity)
{
    data = new int[capacity];
    size = 0;
    this->capacity = capacity;
}

void Set::copy(const Set& copied)
{
    size = copied.getSize();
    capacity = copied.getCapacity();
    if (data != nullptr)
        delete[]data;
    data = new int[capacity];
    for (unsigned int i …
Run Code Online (Sandbox Code Playgroud)

c++ boolean overloading this operator-keyword

0
推荐指数
1
解决办法
88
查看次数