this

Java generics: Use this type as return type?

筅森魡賤 提交于 2019-12-21 04:02:30
问题 I'm trying to make an API as user friendly as possible. Let's have: class B extends A {} class A { A setX(){ ...; return this; } } Now this B b = new B().setX(); is invalid, has to be casted: B b = (B) new B().setX(); Is there a way using generics in A to make compiler aware of "this" type and accept the first way - without casting and without passing type parameter at the place where used? (I.e. not new B<B>().setX() , that's ugly.) I KNOW why Java needs retyping in this case. Please no

Return reference from class to this

怎甘沉沦 提交于 2019-12-21 03:41:01
问题 I have the following member of class foo. foo &foo::bar() { return this; } But I am getting compiler errors. What stupid thing am I doing wrong? Compiler error (gcc): error: invalid initialization of non-const reference of type 'foo&' from a temporary of type 'foo* const' 回答1: this is a pointer. So it should be return *this; 回答2: As Naveen points out, you need to return *this . Just a quick tip though: a way to figure out what somewhat obscure compiler errors mean is to try compiling on a

console.log() called on object other than console

我的未来我决定 提交于 2019-12-21 03:09:15
问题 I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it. For example: const callWithTest = callback => callback('test'); callWithTest(console.log); // That didn't use to work. callWithTest(console.log.bind(console)); // That worked (and works) fine. See Uncaught TypeError: Illegal invocation in javascript. However, recently I noticed that console.log() works fine even when called on

Jquery - Expand image height and width 20% on hover

僤鯓⒐⒋嵵緔 提交于 2019-12-21 02:45:09
问题 Evening all - Whats the best way to access an image's height and width dynamically . I want to add 20% to an images width and height and animate when the surrounding div is hovered, I guess I need to use 'this', but not sure how to access the image. Any help greatfully received. Cheers Paul 回答1: You could do something like this, using .height() and .width() with function arguments: $("img").hover(function() { $(this).height(function(i, h) { return h * 1.2; }) .width(function(i, w) { return w

Is std::move(*this) a good pattern?

别说谁变了你拦得住时间么 提交于 2019-12-20 11:07:09
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

Is std::move(*this) a good pattern?

白昼怎懂夜的黑 提交于 2019-12-20 11:05:28
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

What scope is 'this' of a node.js object when it gets called at the top-level?

假如想象 提交于 2019-12-20 07:26:48
问题 I read "Why and how to bind methods in your React component classes?" and grabbed the basic idea of how different is this by the scopes it gets called. I made a simple class to test this. class Something { constructor(some) { this.some = some; } print() { console.log( "print from ", this, this === undefined ? "!!this is undefined!!" : this.some ); } } From what I understood, the scope of this can be different although it is from the same object (instance). const some = new Something("is");

HTML Onclick with $(this)

孤人 提交于 2019-12-20 06:19:04
问题 I have a conflict with calling the general jQuery click function and so I need to call an onclick event on the HTML element itself while still being able to use $(this) $('.item-main-section, .item-status').click( function () { var slideHeight = $(this).parent().children('.item-slide-section').height(); if ($(this).parent().height() > 50) { $(this).parent().animate({height: '50px'}, 300); $(this).parent().children('item-slide-section').animate({bottom: '0px'}, 300); } else { $(this).parent()

Java - when “this” is the only way to go?

微笑、不失礼 提交于 2019-12-20 05:46:06
问题 The following code runs for both var = putVar; & this.var = putVar; I understand: "this" is used to identify that - "put this value for just 'my' object". When both work, why do people usually use "this" in setters? code: public class PlayingWithObjects { public static void main(String[] args) { SomeClass classObj = new SomeClass(10); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj.setVar(20); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj = new

passing this as parameter in static method

落爺英雄遲暮 提交于 2019-12-20 05:25:17
问题 I'm having some trouble with some code in Visual C# for Windows Phone The trouble is not that it does not work, because it does, but I don't understand how =P Within a static class, a static method is created, which gives itself as a parameter: public static void MethodONe( this Timeline animation ) { //this class does not extend the TimeLine class, and is not connected to it in any //such way. animation.MethodTwo( ); } public static void MethodTwo( this Timeline animation ) { someCode( ); }