this

Maintaining the value of 'this' when using event listener

做~自己de王妃 提交于 2019-12-20 05:12:38
问题 I have the following function: onDocumentKeyUp: function (e) { if (e.keyCode === 27) { this.deactivate(); } } I wanted to bind this like so: $(document).on('keyup', onDocumentKeyUp); However, when I do that, the this inside the onDocumentKeyUp function refers to the document. I solved this by doing: var self = this; $(document).on('keyup', function(e) { self.onDocumentKeyUp(e); }); This works, but something tells me there's a cleaner way to pull this off. Would .call() or .apply() somehow.

Why different behavior for “TYPE* const” pointers?

倖福魔咒の 提交于 2019-12-20 04:49:46
问题 Below code is dealing with a TYPE* const pointer. struct D { void Check () { D* const p = new D; // 2nd test is "p = 0;" cout<<"p = "<<p<<endl; (D*&)p = new D; cout<<"p = "<<p<<endl; // prints 0, "p = 0;" at declaration } }; int main () { D o; o.Check(); } My questions are, If you initialize with 0 , then even though typecasting next time will not work. Is doing such typecasting is undefined behavior ? this pointer is also of TYPE* const type, then why compiler doesn't allow the same

error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers

吃可爱长大的小学妹 提交于 2019-12-20 04:24:24
问题 I'm having some problems compiling my code. It says, error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers part of the code where the error shows up double getR() { return this->r; } double getG() { return this->g; } double getB2() { return this->b2; } also here rez.r = this->r / 2 + a.getR() / 2; rez.g = this->g / 2 + a.getG() / 2; rez.b2 = this->b2 / 2 + a.getB2() / 2; Any ideas? THAT WAS FIXED. Same error on this part of code

Typescript: private member is suddenly undefined

北战南征 提交于 2019-12-20 03:38:09
问题 So, I've got a basic Typescript application, that shouldn't actually cause any major problems, but it seems, that something's going wrong here and I have no idea what. I do have this private member minUpdateRate in my GameContainer class, that is initialized in the constructor. This seems to go well, because when GameContainer.start() is called, the console.log() method will print out 1 . However, when the GameContainer.render() method is called, it seems to be out of the scope or something,

what does $this mean within a class definition? [closed]

爱⌒轻易说出口 提交于 2019-12-20 03:14:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . As I continue to try and improve myself as a junior PHP developer, I have started to try break down other peoples work. I find it helps me understand, as well as giving me ideas. Two things I do not get, in a PHP class, what $this means, and what array($this,'some_function') means when I would expect a function

Does 'this' refer to the element that called this function?

倾然丶 夕夏残阳落幕 提交于 2019-12-20 03:00:36
问题 In the snippet below I use $(this) to refer to the element in which the function is being called from. I know it is not correct because I printed out the values and it gave me 'undefined'. How do I refer to the input element? $(function() { $( ".datepicker" ).datepicker({ onSelect: function (date, obj){ if(confirm('Is this correct?: '+ date )) { $.post('edit.php', { "row": $(this).data('id'), "date":date, "field":$(this).name, "ajax":'true' }); } } }); }); Here is the html element: <input

How to change what an ES6 arrow function's 'this' points to?

╄→гoц情女王★ 提交于 2019-12-20 01:58:11
问题 In the traverse npm package there's this example var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; traverse(obj).forEach(function (x) { if (x < 0) this.update(x + 128); }); Inside the callback function you can call this.update . I know in these kinds of cases you should use the regular (not ES6 arrow) style of function definitions like shown above. But out of curiosity, how would you make that code work with ES6 arrow function syntax? If I try as follows I get TypeError: Cannot

What is the difference in the invocation of 'this' in these examples?

跟風遠走 提交于 2019-12-20 01:55:11
问题 I'm reading Crockford's 'JS: The Good Parts'. He has two examples using this and I don't understand why in one instance he uses this and in another he uses that . The first example: String.method('deentify', function() { var entity = { quot: '"', lt: '<', gt: '<' }; return function() { return this.replace(/&([^&;]+);/g, function (a, b) { var r = entity[b]; return typeof r === 'string' ? r : a; } ); }; }()); document.writeln('<">'.deentify()); The second example: Function.method('curry',

Why “this” inside of a function's returning object window

一笑奈何 提交于 2019-12-20 01:41:51
问题 there is two type of scope in javascript named function scope global scope now i am executing this code function abc() { alert(this); } abc(); abc call returning me [object Window] Why?? function makes another scope so why it is representing window 回答1: this , inside any function, will be the object on which the function is invoked. In your case, you are not invoking it on any object. So, by default this refer to global object, in your browser, it is the window object. But in strict mode, if

this is not a real pointer?

☆樱花仙子☆ 提交于 2019-12-19 22:09:00
问题 I am reading something about virtual table. When it comes to pointer __vptr , it is stated that by the author Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. What does it mean here by saying this is actually a function parameter? And this is not a real pointer? 回答1: Both pointers are real in the sense that they store