this

React: this is null in event handler

蹲街弑〆低调 提交于 2019-11-29 00:22:44
问题 I have a LoginForm component. I want to check before submit, that both loginName and password is set. I tried with this code (a lot of stuff omitted): class LoginForm extends Component { constructor() { super(); this.state = { error: "", loginName: "", password: "", remember: true }; } submit(e) { e.preventDefault(); if(!this.state.loginName || !this.state.password) { //this is null this.setState({ error: "Fill in both fields" }); } else { console.log("submitting form"); } } render() { return

Assignment of a struct value to this keyword

爱⌒轻易说出口 提交于 2019-11-29 00:09:17
I was recently looking into internals of CancellationToken structure and discovered a bit of weird construct (to be more precise, assignment of value to this keyword). Code of one of its constructors is as following: public CancellationToken( bool canceled ) { this = new CancellationToken(); if ( canceled ) { this.m_source = CancellationTokenSource.InternalGetStaticSource( canceled ); } } What is the meaning of line on which the assignment to this keyword occurs? Please note that assignment to this keyword is not possible for classes - error Cannot assign to '<this>' because it is read-only

What is the current element in Javascript? [duplicate]

有些话、适合烂在心里 提交于 2019-11-28 23:43:55
This question already has an answer here: How may I reference the script tag that loaded the currently-executing script? 13 answers How can I find out what the element is that a <script> sits in? As an example, let's take this <div> <script type="text/javascript"> var time = new Date(), hrs = time.getHours(), min = time.getMinutes(); document.write('It is '+hrs+":"+(min<10?'0':'')+min); </script> </div> Then if I want to change this to something more modern, how can I find out what element we're in? So I want to write, for instance in jQuery $(thisdiv).html('It is '+hrs+":"+(min<10?'0':'')+min

Call parent function which is being overridden by child during constructor chain in JavaScript(ES6) [duplicate]

泄露秘密 提交于 2019-11-28 23:41:26
问题 This question already has an answer here : Parent constructor call overridden functions before all child constructors are finished (1 answer) Closed 4 years ago . I've encounter a problem below with JavaScript(ES6) class A{ constructor(){ this.foo(); } foo(){ console.log("foo in A is called"); } } class B extends A{ constructor(){ super(); this.foo(); } foo(){ console.log("foo in B is called"); } } What I expect is foo in A is called foo in B is called But actually it is foo in B is called

Using 'this' keyword in Java constructors [duplicate]

这一生的挚爱 提交于 2019-11-28 23:40:48
This question already has an answer here: What is the meaning of “this” in Java? 18 answers I am confused with the this keyword in Java. If a class has two constructors and we use the this keyword in some method, the object represented by this is instantiated using which of the two constructors? You have to distinguish between this. and this() , so to speak: Most of the time, you use this as the reference to the current object, i.e. the reference of this object is replaced at runtime for this . For instance, if you use this as parameter or reference this.someMember . You can have different

How to pass context to forEach() anonymous function [duplicate]

[亡魂溺海] 提交于 2019-11-28 23:32:09
This question already has an answer here: The invocation context (this) of the forEach function call 5 answers What's the modern and correct way to pass the this context to an anonymous forEach function? function Chart() { this.draw = function(data) { data.forEach(function(value) { //do something with values console.log(this); //question: how to get Chart instead of global scope? )}; }); }; Store the current this in some other variable in Chart like this function Chart() { var self = this; this.draw = function(data) { data.forEach(function(value) { //do something with values console.log(self);

What does 'return *this' mean in C++?

孤街醉人 提交于 2019-11-28 21:43:21
I'm converting a C++ program to C#, but this part has me confused. What does return *this mean? template< EDemoCommands msgType, typename PB_OBJECT_TYPE > class CDemoMessagePB : public IDemoMessage, public PB_OBJECT_TYPE { (...) virtual ::google::protobuf::Message& GetProtoMsg() { return *this; } } How would it translate into C#? this means pointer to the object, so *this is an object. So you are returning an object ie, *this returns a reference to the object. Watch out that if you try to use return *this; on a function whose return type is Type and not Type& , C++ will try to make a copy of

Why can iterators in structs modify this?

夙愿已清 提交于 2019-11-28 21:36:58
I discovered that iterator methods in value types are allowed to modify this . However, due to limitations in the CLR, the modifications are not seen by the calling method. ( this is passed by value) Therefore, identical code in an iterator and a non-iterator produce different results: static void Main() { Mutable m1 = new Mutable(); m1.MutateWrong().ToArray(); //Force the iterator to execute Console.WriteLine("After MutateWrong(): " + m1.Value); Console.WriteLine(); Mutable m2 = new Mutable(); m2.MutateRight(); Console.WriteLine("After MutateRight(): " + m2.Value); } struct Mutable { public

Why can I not use “super” variable from a static context, even though “super” refers to the parent class and NOT a class instance, unlike “this”?

微笑、不失礼 提交于 2019-11-28 21:33:18
I'm talking java language. Variable "this", when used inside a class, refers to the current instance of that class, which means you cannot use "this" inside a static method. But "super", when used inside a class, refers to the superclass of that class, not an instance of the superclass, which should mean that you can use "super" inside a static method. But it turns out you cannot. A possible explanation would be to say that "super" also refers to an instance of the superclass, but I can't see why it should... Here is the section in the JLS about the super keyword: http://docs.oracle.com/javase

Why doesn't the compiler at least warn on this == null

坚强是说给别人听的谎言 提交于 2019-11-28 21:16:29
Why does the C# compiler not even complain with a warning on this code? : if (this == null) { // ... } Obviously the condition will never be satisfied.. Because you could override operator == to return true for that case. public class Foo { public void Test() { Console.WriteLine(this == null); } public static bool operator ==(Foo a, Foo b) { return true; } public static bool operator !=(Foo a, Foo b) { return true; } } Running new Foo().Test() will print "True" to the console. The other question here is: why doesn't the compiler issue a warning for ReferenceEquals(this, null) ? From the bottom