this

JS class without constant reference to “this” for member access

喜你入骨 提交于 2019-12-10 14:56:31
问题 My typical JS class structure looks like this: MyClass = function(args) { this.myProp1 = undefined; this.myProp2 = args[0]; //...more member data this.foo = function() { return this.myProp1 + this.myProp2; //<- the problem. } //...more member functions } //if MyClass extends a superclass, add the following... MyClass.prototype = Object.create(MySuperClass.prototype); MyClass.prototype.constructor = MyClass; ...My ongoing annoyance with JS is that I seem to have to use this continuously in

jQuery callback - strict violation

喜你入骨 提交于 2019-12-10 13:43:25
问题 I get the basic idea about this not being in a method when in strict mode outlined here, but it gets a bit erudite, to be honest. So, in more prosaic terms: I have a handler like so: $('.myClass').one('keyup', function() { var $this = $(this); etc etc }); I want to change it to this: function myFunction () { var $this = $(this); etc etc }; $('.myClass1').one('keyup', myFunction); $('.myClass2').one('keyup', myFunction); etc It doesn't like it because in strict mode because I'm using this

When to use “this” in Java

删除回忆录丶 提交于 2019-12-10 13:40:02
问题 I apologize for my trivial and probably silly question, but I am a bit confused as to when to use the "this" prefix when using a method or accessing something. For example, if we look at #4 here: http://apcentral.collegeboard.com/apc/public/repository/ap_frq_computerscience_12.pdf And we look at the solutions here: http://apcentral.collegeboard.com/apc/public/repository/ap12_computer_science_a_q4.pdf We see that one solution to part a) is public int countWhitePixels() { int whitePixelCount =

C++ and FULLY dynamic functions

久未见 提交于 2019-12-10 13:34:29
问题 I have a problem with detours. Detours, as you all know, can only move among 5 bytes of space (i.e a 'jmp' call and a 4 byte address). Because of this it is impossible to have the 'hook' function in a class (a method), you cannot supply the 'this' pointer because there is simply not enough space (here's the problem more thoroughly explained). So I've been brainstorming all day for a solution, and now I want your thoughts on the subject so I don't begin a 3-5 day project without knowing if it

Is this trick, to make calling shared_from_this() in the constructor 'just work', dangerous?

痞子三分冷 提交于 2019-12-10 13:33:00
问题 Question for the C++ experts. We all know that calling shared_from_this() in the class constructor will result in a bad_weak_ptr exception, because no shared_ptr to the instance has been created yet. As a work-around for that, I came up with this trick: class MyClass : public std::enable_shared_from_this<MyClass> { public: MyClass() {} MyClass( const MyClass& parent ) { // Create a temporary shared pointer with a null-deleter // to prevent the instance from being destroyed when it // goes out

Difference between context [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-10 11:32:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Android - what's the difference between the various methods to get a Context? I want to know what's the difference between using this , ClassName.this , getApplicationContext() or myContext ? What are the effects of using each as context in the Toast below? public class ClassName extends Activity { final ClassName myContext = this; ... public void onCreate(Bundle savedInstanceState) { ... button

Intern: loop on Promise.<Array.<leadfoot/Element>>

一个人想着一个人 提交于 2019-12-10 10:23:18
问题 Let's say I have the following DOM structure, for simplicity: <div class='myparent'> <div class='child'> <div class="label">A</div> <div class="ico"/> </div> <div class='child'> <div class="label">B</div> <div class="ico"/> </div> <div class='child'> <div class="label">C</div> <div class="ico"/> </div> </div> I would like to loop within all child Element returned by the function findAllByCssSelector('.child'). In particular, I would click on the ico div subelement ONLY if the label of the div

Difference between adding a method to a class with the arrow symbol and without in ES6?

蓝咒 提交于 2019-12-10 08:37:58
问题 I have recently come across two ways of adding a method to a class in javascript ES6. Here they are in a nutshell: class SomeClass { someMethod(arg) { console.log(this.anotherMethod); // This will produce an error because 'this' is undefined here. } anotherMethod = (arg) => { // does stuff } } Can someone give me a good explanation of what is happening here, and the meaning of the syntax? I understand from the babel tutorial that the arrow => in ES6 indicates a function. But something else is

Javascript static/singelton - this vs _this vs object name

依然范特西╮ 提交于 2019-12-10 06:58:09
问题 This is a question about performance and best practice. Assuming I have a js object that encapsulates a large number of helper methods. The object is being treated as a static class, meaning it is never instantiated and all its methods are basically helper methods. When using events and jQuery, the object's this scope keeps changing, and since it has a fairly large number of methods I am wondering what is best practice - saving this into _this at the beginning of each method or simply use the

Access to static properties via this.constructor in typescript

℡╲_俬逩灬. 提交于 2019-12-09 14:10:23
问题 I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor , but in typescript this.constructor.prop causes error " TS2339: Property 'prop' does not exist on type 'Function' ". 回答1: but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'". Typescript does not infer the type of constructor to be