this

Why is 'this' undefined inside class method when using promises? [duplicate]

孤街浪徒 提交于 2019-11-26 04:40:31
问题 This question already has answers here : setTimeout and “this” in JavaScript (5 answers) Closed 3 years ago . I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3 . Is there a more correct way to write this code? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // ...q stuff... console.log(this.options); // logs \"opts\"

Why can't we use 'this' keyword in a static method

丶灬走出姿态 提交于 2019-11-26 04:39:30
class Sub { static int y; public static void foo() { this.y = 10; } } I understand that this represents the object invoking the method and that static methods are not bound to any object. But in the above mentioned case, the variable y is also static. If we can invoke static method on class object, why can't we allow static methods to set the static variables of the class. What is the purpose of this additional constraint? Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!).

this operator in javascript

左心房为你撑大大i 提交于 2019-11-26 04:26:48
问题 Suppose I have JavaScript code like myClass = function(){ function doSomething(){ alert(this); // this1 } } alert(this); //this2 What those two \'this\' objects are refer for?? 回答1: 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

Can “this” ever be null in Java?

与世无争的帅哥 提交于 2019-11-26 03:57:11
问题 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? 回答1: 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

'this' does not work properly in another event. I'm clueless as to why [duplicate]

我怕爱的太早我们不能终老 提交于 2019-11-26 03:45:52
问题 This question already has answers here : Javascript “addEventListener” Event Fires on Page Load [duplicate] (2 answers) Closed 2 years ago . Short story, I don\'t know why it\'s not working, I\'ve tried Console.Log() to figure out what \'this\' is and the event just keeps passing window. It\'s a click event that\'s suppose to activate effects on a certain figure in this carousel, which is why I can\'t just individually search for class (at least to my knowledge). Any fix from the smarter? var

TypeError: Cannot read property 'setState' of undefined

蓝咒 提交于 2019-11-26 03:45:42
问题 I am trying to setState of a component after a ajax callback receives data from REST api. here\'s my code for the component constructor constructor(props) { super(props); this.state = { posts: [] }; this.getPosts = this.getPosts.bind(this); } Then I have a componentDidMount method that looks like following. componentDidMount() { this.getPosts(); } Now here\'s my getPosts function where I am doing the ajax request. getPosts = () => { $.ajax({ type: \'get\', url: urlname, success: function(data

Does using $this instead of $(this) provide a performance enhancement?

孤街醉人 提交于 2019-11-26 03:34:27
问题 Assume I have the following example: Example One $(\'.my_Selector_Selected_More_Than_One_Element\').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff(); $(this).myStuff(); $(this).theirStuff(); $(this).children().each(function(){ howMuchStuff(); }); $(this).tooMuchStuff(); // Plus just some regular stuff $(this).css(\'display\',\'none\'); $(this).css(\'font-weight\',\'bold\'); $(this).has(\'.hisBabiesStuff\').css(\'color\',\'light blue\'); $(this)

“this” keyword in event methods when using JavaScript prototype object

故事扮演 提交于 2019-11-26 03:25:45
问题 I\'m trying to access the member variables of a prototype class in JavaScript in an event handler -- something I\'d typically use the \"this\" keyword for (or \"that\" [copy of this] in the case of event handlers). Needless to say, I\'m running into some trouble. Take, for example, this HTML snippet: <a id=\"myLink\" href=\"#\">My Link</a> And this JavaScript code: function MyClass() { this.field = \"value\" this.link = document.getElementById(\"myLink\"); this.link.onclick = this.EventMethod

Java - when to use &#39;this&#39; keyword [duplicate]

依然范特西╮ 提交于 2019-11-26 03:25:08
问题 This question already has answers here : When should I use “this” in a class? (17 answers) Closed 4 years ago . What is the best practise for using the this keyword in Java? For example, I have the following class: class Foo { Bar bar; public Foo(Bar bar) { this.bar = bar; } } That\'s fine and all, but Java is clever enough to know what is happening if I change the statement in the constructor to bar = bar; So why use the this keyword? (I realise in some situations, it\'s totally necessary to

How do I pass the this context to a function?

浪子不回头ぞ 提交于 2019-11-26 03:06:06
问题 I thought this would be something I could easily google, but maybe I\'m not asking the right question... How do I set whatever \"this\" refers to in a given javascript function? for example, like with most of jQuery\'s functions such as: $(selector).each(function() { //$(this) gives me access to whatever selector we\'re on }); How do I write/call my own standalone functions that have an appropriate \"this\" reference when called? I use jQuery, so if there\'s a jQuery-specific way of doing it,