this

js call static method from class

我是研究僧i 提交于 2019-11-26 17:13:29
问题 I have a class with a static method: class User { constructor() { User.staticMethod(); } static staticMethod() {} } Is there something like this so for static methods (i.e. refer to the current class without an instance). this.staticMethod() so I don't have to write the class name 'User'. 回答1: From MDN documentation Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions. For more please see=>

What's the difference between this and Activity.this

Deadly 提交于 2019-11-26 16:07:14
问题 For example Intent intent = new Intent(this, SecondActivity.class); eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class) Intent intent = new Intent(FirstActivity.this, SecondActivity.class); But that will be correct. Anybody can explain the difference between those two ? Thanks. 回答1: this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats

this operator in javascript

假如想象 提交于 2019-11-26 15:28:14
Suppose I have JavaScript code like myClass = function(){ function doSomething(){ alert(this); // this1 } } alert(this); //this2 What those two 'this' objects are refer for?? The this value in the global execution context, refers to the global object, e.g.: this === window; // true For Function Code, it really depends on how do you invoke the function, for example, the this value is implicitly set when: Calling a function with no base object reference : myFunc(); The this value will also refer to the global object. Calling a function bound as a property of an object : obj.method(); The this

The reason to use JS .call() method?

浪子不回头ぞ 提交于 2019-11-26 15:12:47
问题 I'm interested what's the reason to have call() method in JS. It seems it duplicates usual method of calling this . For example, I have a code with call(). var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " " + what); } f.call(obj, "ate", "food"); The output is "Dog ate food". But the same result I can get assigning the function to the object. var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " "

When does the “fat arrow” (=>) bind to “this” instance

依然范特西╮ 提交于 2019-11-26 14:45:47
问题 The fat arrow can be used in different settings but it somehow doesn't always bind to the instance I want. 回答1: The fat arrow binds at 3 occasions when declaring a method when declaring a function within a method when declaring a function in global context 1. When declaring a method When the Coffeescript compiler encouters the following syntactical pattern within a class declaration class A somemethod: (paramlist) => This will yield the following code within the constructor of class A this

Can “this” ever be null in Java?

回眸只為那壹抹淺笑 提交于 2019-11-26 14:36:07
Saw this line in a class method and my first reaction was to ridicule the developer that wrote it.. But then, I figured I should make sure I was right first. public void dataViewActivated(DataViewEvent e) { if (this != null) // Do some work } Will that line ever evaluate to false? No it can't. If you're using this , then you're in the instance so this isn't null. The JLS says : When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed. If you invoked a method from

Controlling the value of 'this' in a jQuery event

佐手、 提交于 2019-11-26 14:26:28
I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible. During the initialisation of my control I wire up various click events like so jQuery('#available input', this.controlDiv).bind('click', this, this.availableCategoryClick); Notice that I am pasing 'this' as the data argument in the bind method. I do this so that I can get at data attached to the control instance rather from the element that fires the click event. This works perfectly, however i suspect there is a better way Having used Prototype in the past, I remember a bind syntax that

How does event handlers with arrow functions achieve context binding

為{幸葍}努か 提交于 2019-11-26 14:25:48
问题 I know about the general theory of this binding (function call site what matters, implicit, explicit binding, etc...) and the methods to solving the problem of this binding in React so it always points to what I want this to be (binding in constructor, arrow functions, etc), but I am struggling to get the inner mechanisms. Take a look at these two pieces of code: class demo extends React.component { goToStore(event) { console.log(this) } render() { <button onClick={(e) => this.goToStore(e)}

JQuery $(this) selector function and limitations

烂漫一生 提交于 2019-11-26 14:11:59
问题 I need help understanding $(this). Is it possible to narrow the focus of 'this' within the parentheses or does "this" preclude the use of any other attributes? For example: I don't understand why this code: $(this).children("div") can't be rewritten like this: $(this +" div") without having to resort to something like: $('#'+$(this).attr("id")+" div") Also, can you point me to 'this' in the JQuery documentation? It is difficult to use 'this' as a search term for obvious reasons. 回答1: this isn

onClick Function “this” Returns Window Object

泪湿孤枕 提交于 2019-11-26 13:52:10
问题 I've come across a head scratching issue with my JavaScript application. If I write an element like this: <li onClick="alert(this.tagName)"></li> I get "LI." However if I do this: <li onClick="foo()"></li> Where "foo()" is: function foo(){ alert(this.tagName); } I get "undefined." I am away how "this" is supposed to work in regards to attached functions. But, I am baffled because "this" is not picking up the element, but apparently defaulting to "window." I can't figure out why this is