this

Is `this` keyword optional when accessing members in C#?

妖精的绣舞 提交于 2020-01-21 11:01:07
问题 I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName , just memberName works. So is the this keyword optional in the context of member access? I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members? 回答1: Yes, it's optional. The only times you'd have to use it are when you

Arrow functions return this as the window object

允我心安 提交于 2020-01-21 10:09:48
问题 In my program using the function() syntax is returning the this value of a target element, but using an arrow function returns the window object. How are each of these two functions getting their this ? function editTemplates() { //sits within for loop clocksTemplate.gmt[i].addEventListener('keydown', (e) => { console.log(this); //returns window object }); clocksTemplate.gmt[i].addEventListener('keydown', function(e) { console.log(this); //returns element bound to clocksTemplate.gmt });

java “this” keyword proper use

落爺英雄遲暮 提交于 2020-01-21 09:27:09
问题 I have a Fraction class using keyword this in my constructor: public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; adjustSigns(); if(this.denominator == 0 ) { throw new FractionException(" Undefined Fraction "); } } I also have a method : public FractionInterface multiply(FractionInterface secondFraction) { Fraction second = (Fraction) secondFraction; Fraction answer = new Fraction ((numerator * second.numerator), (denominator * second

jQuery - 'this' selector doesn't work inside callback function [duplicate]

江枫思渺然 提交于 2020-01-21 07:14:41
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: $(this) doesn't work in a function I'm writing post removing code in jQuery, The removing itself is made via post-request to backeds, after server returns 200, I want to delete this post on client-side. $('.delete-post').click(function() { $.post($(this).attr('href'), {}, function(data) { $(this).closest('.post').remove(); }); return false; }); However, I've noticed that inside function(data) {...) selector

Passing this to window.onscroll function

岁酱吖の 提交于 2020-01-16 16:47:09
问题 How can I pass this to a function assigned to my window.onscroll event? I am trying to trigger myFunction() when a certain condition is met. I need to check this condition onscroll init() { window.onscroll = function() { if(this.currentItemCount() > this.totalElements){ this.totalElements = this.currentItemCount(); this.myFunction(); } }; } However I get an error that this.currentItemCount() is not a function. I know that I need to pass this to window.onscroll but I cannot figure out the

Typescript Knockout best way to retain 'this'?

人盡茶涼 提交于 2020-01-16 02:56:42
问题 This is an extra question to this TypeScript and Knockout binding to 'this' issue - lambda function needed? where two methods of retaining this inside a Knockout function is proposed: class viewmodelclass { declaredinconstructor: (args) => void; constructor(public someobjects: Array) { this.declaredinconstructor=(args) =>{ } } declaredoutside(args) { } declaredoutsidecorrectly = (args) => { console.log(this); }; } Producing this Javascript: var viewmodelclass = (function () { var _this = this

Using this within functions called with onclick event in Javascript

一个人想着一个人 提交于 2020-01-15 18:46:51
问题 I'm currently building a small Todo list application using vanilla Javascript but I'm having some issues creating a delete button that onClick removes it's parent element. From what I have read, when an onClick is called in Javascript the this keyword can be used to refer to the element that called the function. With this in mind I have the following code: window.onload = initialiseTodo; function addRecord(){ var title = document.getElementById('issueTitle'); var issueContent = document

Java: this keyword preceded by class name

China☆狼群 提交于 2020-01-15 07:16:18
问题 I find a snippet in ArrayList.java from jdk 8: @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } The line: Object[] elementData = ArrayList.this.elementData; looks strange to me. I think ArrayList.this is equivalent to this

Java: this keyword preceded by class name

旧城冷巷雨未停 提交于 2020-01-15 07:16:12
问题 I find a snippet in ArrayList.java from jdk 8: @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } The line: Object[] elementData = ArrayList.this.elementData; looks strange to me. I think ArrayList.this is equivalent to this

Fixed size buffer cannot be directly used from “this” object

六月ゝ 毕业季﹏ 提交于 2020-01-14 07:41:07
问题 I am used a structure to represent pure data. One of the fields is a fixed-size buffer, as shown below. [StructLayout(LayoutKind.Sequential, Pack=2)] unsafe struct ImageDosHeader { ... private fixed ushort _e_res[4]; ... [Description("Reserved")] [DisplayName("e_res[0]")] public ushort e_res_0 { get { ... } set { ... } } ... } Within the get/set functions I tried to do the following but I get "Compiler Error CS1666: You cannot use fixed size buffers contained in unfixed expressions. Try using