this

Why is 'this' a pointer and not a reference?

我与影子孤独终老i 提交于 2019-12-17 02:30:12
问题 I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56 That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35 I'd

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

眉间皱痕 提交于 2019-12-17 00:13:13
问题 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? 回答1: Because this refers to the object instance. There is no object

TypeScript “this” scoping issue when called in jquery callback

▼魔方 西西 提交于 2019-12-16 22:08:14
问题 I'm not sure of the best approach for handling scoping of "this" in TypeScript. Here's an example of a common pattern in the code I am converting over to TypeScript: class DemonstrateScopingProblems { private status = "blah"; public run() { alert(this.status); } } var thisTest = new DemonstrateScopingProblems(); // works as expected, displays "blah": thisTest.run(); // doesn't work; this is scoped to be the document so this.status is undefined: $(document).ready(thisTest.run); Now, I could

When should I make explicit use of the `this` pointer?

二次信任 提交于 2019-12-16 19:57:42
问题 When should I explicitly write this->member in a method of a class? 回答1: Usually, you do not have to, this-> is implied. Sometimes, there is a name ambiguity, where it can be used to disambiguate class members and local variables. However, here is a completely different case where this-> is explicitly required. Consider the following code: template<class T> struct A { int i; }; template<class T> struct B : A<T> { int foo() { return this->i; } }; int main() { B<int> b; b.foo(); } If you omit

Fatal Error For $this variable

亡梦爱人 提交于 2019-12-14 03:24:05
问题 I get a fatal error saying that I can't use $this variable when not in object context: - Fatal error: Using $this when not in object context in C:\xampp\htdocs\ooplr\classes\DB.php on line 29 Essentially, I am trying to design a sophisticated user login system. Here is the code for the DB.php file: <?php class DB{ private static $_instance = null; private $_pdo, $_query, $_error = false, $_results, $_count =0; private function __construct(){ try { $this->_pdo = new PDO('mysql:host='.Config:

jQuery pass $this to function parameter

流过昼夜 提交于 2019-12-14 02:10:22
问题 I have: <img id="leftBubble" class="bubbles" src="left.png" /> <img id="rightBubble" class="bubbles" src="right.png" /> And a hover event like so: $(".bubbles").each(function(){ $(this).hover(function() { pause($(this)); }, function() { play(4000, $(this)); }); }); My pause() function does not seem to be working function pause(pauseMe) { if (pauseMe == $("#leftBubble")) { clearTimeout(timer1); //this is never reached } else if (pauseMe == $("#rightBubble")) { clearTimeout(timer2); //nor this

Typescript + Jquery Ajax + this

限于喜欢 提交于 2019-12-14 00:46:15
问题 I am porting some javascript code over to typescript and I have come across a problem. I have an ajax call which passes an object as context, this object contains some callbacks and some other information which are read out by the success or error callbacks which indicate where the success invocation should be redirected to: function SomeAjaxService { var self = this; self.CheckErrorType = function(...) { // Do something }; var SuccessProxyCallback = function(results) { // Do some stuff with

Using $(this) after .replaceWith()

旧街凉风 提交于 2019-12-14 00:32:42
问题 Please consider the below HTML and Javascript. In the script, I am replacing an a tag with a p tag. I am expecting the alert() function to return the contents of the p tag but instead it returns the contents of the original a tag which no longer exists. How can I reference the new element? HTML: <a href="">This is a link</a> Javascript: $(document).ready(function() { $("a").each(function() { $(this).replaceWith('<p>New Paragraph</p>'); alert($(this).text()); }); }); 回答1: You can't do it

Form errors with handleInputChange in component

你离开我真会死。 提交于 2019-12-13 21:47:07
问题 I'm using map to loop over an array of objects. I'm using this data to populate a form however I'm having trouble with the handleInputChange function. How do I initiate handleInputChange when I'm using a components. The error I get is this.setState is not a function at handleInputChange Path: formComponent.jsx return ( <li> <SingleInput inputType={'text'} title={'Company name'} name={'position.company'} controlFunc={this.props.handleInputChange} content={this.props.company} placeholder={

How to access the correct `this` inside a callback?

本小妞迷上赌 提交于 2019-12-13 20:29:06
问题 I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); }); } // Mock transport object var transport = { on: function(event, callback) { setTimeout(callback, 1000); } }; // called as var obj = new MyConstructor('foo', transport); However, I'm not able to access the data property of the created object inside the callback. It looks like this does not refer to the object that