this

When calling a Javascript function, how do I set a custom value of “this”?

随声附和 提交于 2019-12-07 04:51:34
问题 I'm using jQuery and I have a function that serves as an event callback, and so in that function "this" represents the object that that captured the event. However, there's an instance where I want to call the function explicitly from another function - how do I set what "this" will equal within the function in this case? For example: function handleEvent(event) { $(this).removeClass("sad").addClass("happy"); } $("a.sad").click(handleEvent); // in this case, "this" is the anchor clicked

Unexpected use of 'event' no-restricted-globals when using event.target.id to get id from bind(this)

倖福魔咒の 提交于 2019-12-07 02:50:16
问题 This code works on Codepen: See https://codepen.io/pkshreeman/pen/YQNPKB?editors=0010 However I am trying to use this in my own 'create-react-app' and the error of 'no-restricted-globals' is trigged by event.target.id . What is a workaround for this. How do you get id from 'this' in react other than using the event target? const Elem = (props) =>{ return (<div> <h1 onClick={props.clickon} id="GM"> Good Morning! <br/> {props.name} {props.last} <br /> This is phase three</h1> <button id="btn1"

What does assignment to *this do (*this = val)?

久未见 提交于 2019-12-07 01:40:18
问题 I was browsing Qt sources, and noticed this QUuid &operator=(const GUID &guid) { *this = QUuid(guid); return *this; } I've never seen assignment to "this" before. What does assignment to "this" do? 回答1: That is not an assignment to this but to the object pointed by this . That will effectively call operator=( QUuid const & ) on the current object. 回答2: It just invokes QUuid &operator=(const QUuid& quUid); . 回答3: 'this' is simply a pointer to the object on which the current method is invoked.

The C++ implicit this, and exactly how it is pushed on the stack

我与影子孤独终老i 提交于 2019-12-07 01:19:47
问题 I need to know whether, when a class method in C++ is called, the implicit 'this' pointer is the first argument, or the last. i.e: whether it is pushed onto the stack first or last. In other words, I'm asking whether a class method, being called, is taken by the compiler to be: int foo::bar(foo *const this, int arg1, int arg2); //or: int foo::bar(int arg1, int arg2, foo *const this); By extension therefore, and more importantly, that would also answer whether G++ would push the this pointer

Can this be null in Scala?

醉酒当歌 提交于 2019-12-07 01:18:10
问题 I just read this question and stumbled upon the following quote: Scala treats == as if it were defined as follows in class Any : final def == (that: Any): Boolean = if (null eq this) (null eq that) else (this equals that) The (null eq this) part made me wonder: Is it actually possible to call methods on null pointers? Can this be null in Scala? 回答1: Check out the Scala language specification, namely 6.3 The Null Value chapter: The null value is of type scala.Null , and is thus compatible with

Calling functions inside a map() function in React render() [duplicate]

老子叫甜甜 提交于 2019-12-07 00:15:02
问题 This question already has answers here : React 'cannot read property of undefined' when using map (2 answers) Closed 2 years ago . This is a follow up to my previous question, but how do I call a function inside React's render() method, if I have a map inside that. Example (this code is inside a class that extends React.Component ): getItemInfo(item){ return `${item.Something} ${item.SomethingElse}` } render() { return ( <div className="someDiv"> { collection.map(function (item) { return <div

Passing `this` before base constructors are done: UB or just dangerous?

回眸只為那壹抹淺笑 提交于 2019-12-06 20:20:44
问题 Consider this smallest example (I could think of): struct Bar; struct Foo { Bar* const b; Foo(Bar* b) : b(b) {} }; struct Bar { Foo* const f; Bar(Foo* f) : f(f) {} }; struct Baz : Bar { Baz() : Bar(new Foo(this)) {} }; When passing this to the ctor of Foo , nothing in the hierarchy of Baz has been created, but neither Foo nor Bar do anything problematic with the pointers they receive. Now the question is, is it simply dangerous to give away this in this fashion or is undefined behaviour?

Why `this.synchronized` instead of just `synchronized` in Scala?

偶尔善良 提交于 2019-12-06 16:44:52
问题 In an example of working with JDBC in Scala, there is a following code: this.synchronized { if (!driverLoaded) loadDriver() } Why this.synchronized instead of just synchronized ? 回答1: In scala synchronized is not a keyword, as in java. It is in fact a member of AnyRef, which is scala equivalent for java's Object . So to answer your question, you can either use synchronized or this.synchronized , just as you can do toString or this.toString . 来源: https://stackoverflow.com/questions/7826822/why

why this keyword is used in java interface and what does it refer?

你离开我真会死。 提交于 2019-12-06 15:49:28
I just figured that I can use the this keyword in an interface . So, if this keyword represents current class object reference in a class , then what does it represent in an interface ? interface InterfaceOne { default void display() { this.defaultMethod(); System.out.println("InterfaceOne method displayed"); } default void defaultMethod() { System.out.println("defaultMethod of InterfaceOne called"); } } Even in this case, the this keyword is used in the same context and meaning. One thing you are missing is, that the this keyword represents the current "Object" and not current "Class" . So,

jQuery $(this) syntax question

流过昼夜 提交于 2019-12-06 15:45:06
Is this a valid selector? If not, what's the correct way? $($(this)+' childElement').... Use $(this).find("childrenSelector") This may be what you are looking for: $('childElement', $(this)) Basically it will search for childElement within the context of the current element, this . For more information, see the documentation for the jQuery() function. In particular, the following excerpt explains the second argument and how it is equivalent to using find : By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for