this

How can I maintain control of the this keyword when extending prototypes in jQuery? [duplicate]

随声附和 提交于 2019-12-02 13:33:56
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 4 years ago . I'm implementing a class-like structure in jQuery, but I'm having some trouble when I try to call some of my functions. This is how the structure is setup: MyClass = function(name) { this.init(name); } $.extend(MyClass.prototype, { init: function(theName) { this.myFunction(); // works $('.myclass').each(function(){ this.myFunction(); // doesn't work }); }, myFunction

Can someone explain to me in detail the use of 'this'?

烂漫一生 提交于 2019-12-02 12:26:29
问题 I don't really understand the use of 'this' in Java. If someone could help me clarify I would really appreciate it. On this website it says: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html "Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this." and it gives the following

How to “insert” code before this(…) or super(…)?

瘦欲@ 提交于 2019-12-02 11:55:27
Is there any way to implement preliminary calculations before an invocation of super(...) or this(...) constructor? Consider the following example: public class Test { private final int n; private final int m; private final int[] store; public Test(int n, int m) { /* This is common (most generic) constructor of the class Test. It is desirable to invoke it via this(...) call from any other constructor of this class since it contains some common initialization tasks, which are better to concentrate in one place instead of scattering them throught the class, due to maintainability and security

Is there a functional purpose for setting a variable to 'this'? [duplicate]

假如想象 提交于 2019-12-02 10:54:50
This question already has an answer here: How to access the correct `this` inside a callback? 10 answers As in, sometimes when I look at code by other people, they will go var self = this; or in jquery for example, go var $self = $(this); is there a particular reason for doing so? It preserves the value of this for use in functions defined inside the current function. // Contrived example var myObject = { func: function () { var self = this; setTimeout(bar, 1000); function bar () { alert(this); // `window` alert(self); // `myObject` } } }; myObject.func(); The particular example (not using

Java - when “this” is the only way to go?

三世轮回 提交于 2019-12-02 10:01:54
The following code runs for both var = putVar; & this.var = putVar; I understand: "this" is used to identify that - "put this value for just 'my' object". When both work, why do people usually use "this" in setters? code: public class PlayingWithObjects { public static void main(String[] args) { SomeClass classObj = new SomeClass(10); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj.setVar(20); System.out.println("classObj.getVar: " + classObj.getVar() ); classObj = new SomeClass(30); System.out.println("classObj.getVar: " + classObj.getVar() ); } } class SomeClass {

How to access this variable inside this function

此生再无相见时 提交于 2019-12-02 10:01:49
I have private variables in constructor and public variables in the class . I refer to this variables and functions using this keyword. I am getting undefined if I try to access this variables inside this function. I am new to typescript , how can I access this variable inside this function? Technology : Typescript, Angular 2, Angular 1.6.5, JavaScript admin-company-settings.ts import { Component } from '../../../../common/extentions/to-angular2'; import { Http } from '@angular/http'; export class AdminCompanySettings { public company: any; constructor(private $http: ng.IHttpService) { // }

Javascript: Creating a persistently bound function

可紊 提交于 2019-12-02 09:34:47
问题 I realise a question like this is asked pretty frequently (I've probably read every one of them over the past few days trying to understand how to fix this) - but in this case, while I'm fairly confident I know why this is happening, I'm struggling to implement an actual solution. I'm building a small application using Node.js but having trouble with creating an object with prototype functions that won't lose their binding when they're passed around. Here's what I have so far: foo.js var Foo

How to get List<List<String>> with this keyword?

淺唱寂寞╮ 提交于 2019-12-02 09:29:18
I have fragment where I get the string in List and send it as Bundle to the activity. For Example I get a string and and send it to an activity by following way: public class ViewPagerAdapter extends FragmentStatePagerAdapter { private CharSequence contentTitle[]; public ViewPagerAdapter(Response<album> response) { List<String> listcontentTitle = new ArrayList<>(); List<List<String>> latest_list = new ArrayList<>(); List<String> latestdate = new ArrayList<>(); List<String> latestcomment = new ArrayList<>(); for (int i = 0; i < 5; i++) { listcontentTitle.add(String.valueOf(response.body()

Is there a way to have lexical `this` in methods using the ES6 shorthand method notation?

主宰稳场 提交于 2019-12-02 08:51:03
问题 First question on SO and I hope I’m not duplicating anything; I’ve looked at other questions and think mine is different enough to warrant asking. Basically, is there a way to have the this that’s in the method body of a method written using the shorthand notation to be either lexical or bound to a specific value? The motivation for this comes from wanting to use the ES6 method shorthand when I was implementing the iterator protocol, in which the @@iterator method, when called, returns an

What scope is 'this' of a node.js object when it gets called at the top-level?

筅森魡賤 提交于 2019-12-02 08:20:31
I read "Why and how to bind methods in your React component classes?" and grabbed the basic idea of how different is this by the scopes it gets called. I made a simple class to test this. class Something { constructor(some) { this.some = some; } print() { console.log( "print from ", this, this === undefined ? "!!this is undefined!!" : this.some ); } } From what I understood, the scope of this can be different although it is from the same object (instance). const some = new Something("is"); some.print(); // 'this' is defined. 'this.some' is 'is'. const going = some.print; going(); // 'this' is