this

Overhead when using keyword this?

断了今生、忘了曾经 提交于 2019-12-24 23:16:10
问题 The scope of my question is solely ASP.NET, as the answer may be different for Java and any other C based language. How much overhead is involved when using the keyword "this" within a class to dereference a property? It seems that I've seen certain sources try to discourage the use of "this" for dereferencing, but generaly I've just ignored them until now. 回答1: I think it is just a style issue. As stated in the answer to this question, the compiler injects 'this' into implicit uses of this.

TypeScript 'this' scoping issue

a 夏天 提交于 2019-12-24 09:27:11
问题 My code: export class WordService { constructor(private http:Http, private storage:Storage){} impulseData={ get():Promise<any>{ return this.storage.get('impulseData'); } }; } When I call myWordService.impulseData.get() , I found this.storage is undefined . So how can I reach the property storage in impulseData.get ? I guess this problem was caused by the scoping of this . Maybe I should let the this inside and outside impulseData share the same scope? Update: Thanks to Suren Srapyan's answer,

jQuery removeClass and addClass within a function

别等时光非礼了梦想. 提交于 2019-12-24 09:13:04
问题 I got following menu: <ul id="nav" class="nav"> <li> <a class="navitem active" href="javascript:loadTab();">My Profile </a> </li> <li> <a class="navitem search" href="javascript:loadTab();"> Search </a> </li> <li> <a class="navitem" href="javascript:loadTab();">Favorites </a> </li> </ul> And my function (loaded in a .js file in the header): function loadTab() { jQuery(".navitem").removeClass("active"); jQuery(".navitem").click(function () { jQuery(this).addClass("active"); }); } Removing the

Uses of “this” Keyword

久未见 提交于 2019-12-24 07:39:07
问题 I'm studying the uses of this and I think I understand it, at least partially, like when to solve ambiguity and to call methods from the "current" class, but one example from the book I'm reading(Head First C#) is giving me a hard time: public void SpeakTo(Elephant whoToTalkTo, string message) { whoToTalkTo.TellMe(message, this); } In this method, is it safe to say that the this has the function of always refering to whatever object calls the method SpeakTo() , regardless of which class the

Keep babel class as `this` when using a member function in `setTimeout`

纵然是瞬间 提交于 2019-12-24 05:47:19
问题 I have an ES2015 class, call it Foo , which has at least two member functions, bar and baz . In bar there is a call to setTimeout whose first parameter is this.baz . Works fine up to here, I inspected it in the debugger, and this does refer to the instance of my class. (Actually since I'm using babel, I end up with a _this = this substitution beforehand but anyway the right thing is being passed into the setTimeout , confirmed.) The problem is when the setTimeout callback fires, it calls the

Extend setter default object

巧了我就是萌 提交于 2019-12-24 05:26:09
问题 Like you all know a button is a button... click, up, down, do this, do that. So I wrote some default button behavior "class/object". external default button.js: function Button(parent) { var self = this; this.enabled = true; this.visible = true; ... this.initialized = false; f_createButton(parent, self); this.initialized = true; ... } Button.prototype = { get initialized () { return this._initialized; }, set initialized(bool){ this._initialized = bool if(this.initialized === true) { ... do

Fix for JSLint “Unexpected 'this'.” error?

末鹿安然 提交于 2019-12-24 05:12:23
问题 I am trying to get the following code to become jslint-compliant, but am stuck on the following two errors: Expected to see a statement and instead saw a block. and Unexpected 'this'. What changes should I make to my code to make JSLint happy? var pvAccess = {}; pvAccess.Func = function () { "use strict"; function AccessPV(name, rValue, wValue) { var url = '/goform/ReadWrite', data = 'redirect=/response.asp&variable=' + escape(name), xmlHttp = null, wValue = null; if (rValue !== null &&

Toast.makeText(getApplicationContext(), “String”, Toast.LENGTH_LONG); ==>Here getApplicationContext() cannot change to “this”?

孤街浪徒 提交于 2019-12-24 02:56:14
问题 First the format of Toast.makeText(): public static Toast makeText (Context context, CharSequence text, int duration) the first argument is Context , the function getApplicationContext() also return the current context, everything is ok, but IMO, the getApplicationContext() can also be replaced with this, just as follows: public class ContextMenuResourcesActivity extends Activity { /** Called when the activity is first created. */ private Button b1; @Override public void onCreate(Bundle

Coffeescript - 'this' is always replaced by '_this' in fat arrow callback

爷,独闯天下 提交于 2019-12-24 02:45:22
问题 I'm wondering is possible somehow to prevent this keyword to be transformed into _this inside fat arrow callback ( => )? For example: class someClass someMethod: -> $(document).on 'click', '.myclass', (e) => # doing things with right context this, it's ok @anotherMethod() @oneMoreMethod() # but here I need jQuery ``this`` pointing to element $el = $ this # this is transformed into ``_this`` :( Maybe I missed some option or operator? UPDATE I know about the trick like self = this , but I

Refering to the class itself from within a class mehod in Objective C

泄露秘密 提交于 2019-12-24 02:39:13
问题 I hope i did not skip this part in the ObjC manual but is it possible to refer to a class from within one of its class methods? Like in PHP you would use "this" to refer to the current instance, while "self" refers to the instance's class the ObjC equivalent of "this" would be "self", so what would be the ObjC equivalent of PHP's "self", if there is one? 回答1: Inside a class method, self refers to the current class (the class's Class object). Inside an instance method, self refers to the