this

What does “this” refer to in the following javascript?

丶灬走出姿态 提交于 2019-12-11 04:32:45
问题 DISCLAIMER: I am asking about a specific use of this , not what this is used for in general. So, please no google/copy/paste speed answers (: I have the javascript/jquery code below: var req = {}; function getData() { var fromPage = 0; var toPage = 1; req = $.ajax({ url: "/_vti_bin/lists.asmx", type: "POST", dataType: "xml", data: xmlData, complete: onSuccess, error: function (xhr, ajaxOptions, thrownError) { alert("error: " + xhr.statusText); alert(thrownError); }, contentType: "text/xml;

Can a class be nullified from within the class itself?

做~自己de王妃 提交于 2019-12-11 04:09:07
问题 For example, is this code valid?. class abc{ int x,y; abc(int x,int y){ this.x=x; this.y=y; while(true) update(); } public void update(){ x--; y--; if(y==0) this=null; } } If the above is not valid, then please explain why. I am in need of a class that after certain iterations ceases to exist. Please suggest alternatives to the above approach. 回答1: No. The reason is that you do not make object null. When you say obj = null; You just put null to variable that previously hold reference to

“this” keyword inside closure

做~自己de王妃 提交于 2019-12-11 04:04:14
问题 I've seen a bunch of examples but can't seem to get some sample code to work. Take the following code: var test = (function(){ var t = "test"; return { alertT: function(){ alert(t); } } }()); and I have a function on window.load like: test.alertT(); That all works fine. However, when I try to explicitly set the context of t inside the alert() in alertT , I just get undefined. I've tried: var that = this; alert(that.t); //undefined I've tried: return { that: this, alertT: function(){ alert

Having the correct value of 'this' in JS

心不动则不痛 提交于 2019-12-11 03:39:46
问题 I have two Javascript "objects" similar to so.... var Object2 = new (function() { this.FetchData = function(callback) { // do some stuff callback(data); }; }); var Object1 = new (function() { this.DisplayStuff = function() { }; this.LoadData = function() { Object2.FetchData(this.OnData); }; this.OnData = function(data) { // this == window this.DisplayStuff(); // doesn't work }; }); When Object1 receives the callback to OnData, the value of "this" is set to window. Is there any way I can get

Use of this Javascript

对着背影说爱祢 提交于 2019-12-11 03:29:24
问题 New to JS.. can anyone tell me if the use of this is appropriate in the below function: var Vector = (function()... this.prototype.add = function(someVector2){ var tmpVect = this; tmpVector.x += someVector2.x; tmpVector.y += someVector2.y; return tmpVect; }; In the sense that 'var tmpVect = this' will result in a local Vector variable with the x & y attributes of the vector that called the function? Cheers 回答1: I would rewrite this like so:(based on your comments) var Vector = function(){ }

Inconsistent behaviour of “this” in JavaScript strict mode

微笑、不失礼 提交于 2019-12-11 03:28:07
问题 Update For clarity: @FelixKing: Yes, I expected this to still be undefined when calling window.foo() , and here's why: since, in JavaScript: function foo() { console.log('I am a function'); } Is ( almost ) the same thing as: var foo = function() { console.log('I am a function'); } and foo === window.foo evaluates to true, I'd expect them both to behave alike. If functions are variables, and JS falls back to the global object (inside function x, a variable isn't declared but you use it, JS

What does 'this' mean in a c# constructor? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-11 03:11:24
问题 This question already has answers here : what is 'this' constructor, what is it for (4 answers) Closed 4 years ago . I am very new to c# and I am working through a textbook. The textbook shows this piece of code: public class BankAccount { // Bank accounts start at 1000 and increase sequentially. public static int _nextAccountNumber = 1000; // Maintain the account number and balance for each object. public int _accountNumber; public decimal _balance; // Constructors public BankAccount() :

Using a class method in the Global context has `this` as undefined

人走茶凉 提交于 2019-12-11 03:08:12
问题 I have a class, which has a method that uses this . I 'newed up' an instance of this object and passed on its method to a variable in the global context. If I then call my global function this is undefined. class Tests { logThis() { console.log(this); } } const globalFunc = new Test().logThis; globalFunc(); // undefined Now, if I had just used an object literal then this is is global. const someObject= { logThis2: function() {console.log(this)} } const globalFunc2 = someObject.logThis2;

this refers to current object. But can't understand the below behaviour

孤街浪徒 提交于 2019-12-11 03:06:40
问题 class Person { string name; public Person(string name) { this.name = name; } public void method() { Person gupta = new Person("James"); // Current Object Console.WriteLine(this.name); Person gupta1 = new Person("Peter"); // Current Object Console.WriteLine(this.name); Person gupta2 = new Person("Frank"); // Current Object Console.WriteLine(this.name); } static void Main(string[] args) { Person p = new Person("Jim"); p.method(); Console.ReadLine(); } } This code produced result Jim Jim Jim

AlertDialog show = new AlertDialog.Builder(this) is Undefined

≯℡__Kan透↙ 提交于 2019-12-11 02:21:31
问题 The app intercepts sms messages and displays a Dialog of the message. However I am unable to get my Dialog error resolved in my Test class. What am I doing wrong? (I've also included my other 2 files). ERROR shown in Eclipse: AlertDialog.Builder(Test) is undefined test.java package com.example.firstapp; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony