this

this() and this

試著忘記壹切 提交于 2021-02-05 05:41:35
问题 Does Java has this() ? If so, what's the difference between this and this() ? 回答1: this is reference to current instance. this() is call to default constructor. super() is explicit call to default constructor of super class. 回答2: this is a reference to the current object. this() is a call to the default constructor; it is only legal inside another constructor, and only as the first statement in the constructor. You can also call super() to invoke the default constructor for the superclass

JQuery keyword “this” does not get attribute value

烂漫一生 提交于 2021-02-04 19:41:09
问题 I am playing around with the JQuery keyword this. I have come across something I do not understand. Here is my code: <body> <a id="link_1">jQuery.com</a> <!-- adding JQUERY --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <!-- my JavaScript --> <script> $("a").attr("href", "target_1"); $("a").attr("new_attribute", "new_attribute_value"); $(function ($) { $("a").mouseenter(function(){ alert(this.id); alert(this.href); alert(this.new_attribute); }); }

'thisArg' context not correctly recognized in bound function with TypeScript

痞子三分冷 提交于 2021-01-29 22:12:23
问题 With the below code, TypeScript won't recognize the context of thisArg (which should be a and should have saySomething() method. is there another way to give context of thisArg for TypeScript? The code: class A { saySomething() { console.log('Hello!'); } } class B { constructor(a: A) { const boundSaySomething = this.saySomethingMyself.bind(a); boundSaySomething(); } saySomethingMyself() { this.saySomething(); } } const test = new B(new A()); the console correctly logs Hello , but the the type

'thisArg' context not correctly recognized in bound function with TypeScript

只愿长相守 提交于 2021-01-29 20:57:03
问题 With the below code, TypeScript won't recognize the context of thisArg (which should be a and should have saySomething() method. is there another way to give context of thisArg for TypeScript? The code: class A { saySomething() { console.log('Hello!'); } } class B { constructor(a: A) { const boundSaySomething = this.saySomethingMyself.bind(a); boundSaySomething(); } saySomethingMyself() { this.saySomething(); } } const test = new B(new A()); the console correctly logs Hello , but the the type

How to bind 'this' to an object arrow function?

筅森魡賤 提交于 2021-01-29 12:30:22
问题 Let us suppose we have an object profile with properties name and getName method (arrow function). profile = { name: 'abcd', getName: () => { console.log(this.name); } } I want to call getName method by keeping the arrow function intact, and not changing it to regular function. How can I get the output abcd by calling getName() . You can add expressions inside getName . Will call() or bind() help? If so, how? DO NOT CHANGE THE ARROW FUNCTION TO REGULAR FUNCTION -- EDITED -- I just want to ask

The Binding of “this”

时光毁灭记忆、已成空白 提交于 2021-01-29 03:16:19
问题 I'm learning how the value of "this" binds in JavaScript. In the following example, is it correct to say that "this" binds to the getFriends() method instead of the "details" object, which is the reason this.name = "" instead of this.name = "Joe"? const details = { name: 'Joe', friends: [ 'Bob', 'Alex' ], getFriends: function() { this.friends.forEach( function( friend ) { console.log( this.name + " is friends with " + friend ); } ); } }; details.getFriends(); // Output: // is friends with Bob

How do I access 'this' from within a C# extension method?

自作多情 提交于 2021-01-28 08:57:08
问题 I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member function on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors. Adding this code to my project enabled a cute extension method: using ExtensionMethods; namespace ExtensionMethods { public static class MyExtensions { public static Vector2 NormalizeOrZero(this Vector2 v2) { if (v2 != Vector2.Zero)

How do I access 'this' from within a C# extension method?

大城市里の小女人 提交于 2021-01-28 08:37:33
问题 I've been working with Vector2's and XNA, and I've come to find that calling the Normalize() member function on a Zero Vector normalizes it to a vector of {NaN, NaN}. This is all well and good, but in my case I'd prefer it instead just leave them as Zero Vectors. Adding this code to my project enabled a cute extension method: using ExtensionMethods; namespace ExtensionMethods { public static class MyExtensions { public static Vector2 NormalizeOrZero(this Vector2 v2) { if (v2 != Vector2.Zero)

How calling a callback directly fixes the 'this' in a React Class Component?

左心房为你撑大大i 提交于 2021-01-28 00:37:47
问题 I would like to learn how is the value of 'this' set when a function called in JSX as a callback to an eventHandler. I noticed that when I call it directly there is no issue accessing state without getting the famous undefined 'this' error, like so: import React from "react"; class Accordion extends React.Component { state = { term: "random term" }; onTitleClick() { console.log("Title is clicked"); console.log(this.state.term); } render() { const renderedItems = this.props.items.map((item) =>

Understanding “this” within anonymous function in JavaScript

旧城冷巷雨未停 提交于 2021-01-28 00:33:47
问题 In this post, lots of answers are there discussing the this keyword in JavaScript. However, I am still confuse this in the anonymous function as following // MyModule.js 'use strict'; (function(handler) { // export methods handler.B = B; handler.A = A; function A() { console.log(this); console.log('function A is invoked...'); } function B() { console.log(this); console.log('function B is invoked...'); try { A(); this.A(); } catch (err) { console.log('Exception is ' + err); } } })(module