this

CoffeeScript: How to use both fat arrow and this?

我是研究僧i 提交于 2019-12-12 07:09:26
问题 I have a coffeescript class that has some jquery event listeners. I would like to use the fat arrow => to avoid having to reference the class, but I still need a reference to the element that would usually be used with this . How can I use both? class PostForm constructor: -> $('ul.tabs li').on 'click', => tab = $(this) @highlight_tab(tab) @set_post_type(tab.attr('data-id')) highlight_tab: (tab)-> tab.addClass 'active' set_post_type: (id) -> $('#post_type_id').val(id) 回答1: CoffeeScript links

c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one [duplicate]

∥☆過路亽.° 提交于 2019-12-12 06:03:16
问题 This question already has an answer here : c++ , pthread and static callbacks. “this” returns a pointer to the base class inctead of the derived one (part 2) (1 answer) Closed 3 years ago . I am using as basis this cpp thread class. This I use as a base class for threads. Note that in my case Thread::main() is a virtual function (unlike in the link). So I basically use: class Thread { public: virtual void main() { cout << "This should not be run once derived and redefined." << endl; } void

this.apply method with this as parameter

回眸只為那壹抹淺笑 提交于 2019-12-12 05:29:57
问题 On Douglas Crockford's book The Good Parts, he implements the array push method on this way: Array.prototype.push = function ( ) { this.splice.apply( this, [ this.length, 0 ]. concat(Array.prototype.slice.apply(arguments)) ); return this.length; }; Dont get it how works this. method_name .apply with itself ( this ) as parameter, in the code corresponds to this.splice.apply ; if I use Array.prototype.splice.apply I don't get the correct result. Hope somebody may explain me which is the

Why `this` is not point to js global scope in the code example

限于喜欢 提交于 2019-12-12 04:59:41
问题 Why this is not point to js global scope in the code below ? <html> <head></head> <body> <script type="text/javascript"> var valueHolder = { value: '', setValue: function(newValue) { this.value = newValue; }, getValue: function() { return this.value; } } valueHolder.setValue("hello world"); alert(valueHolder.getValue()); // return "hello world" alert(valueHolder.value); // return "hello world" alert(window.value); // return "undefined" </script> </body> </html> 回答1: Depends on the reference

Accessing “this” Type JavaScript Variables From Other Functions

好久不见. 提交于 2019-12-12 04:58:22
问题 I have an event firing and even though it's inside of the function from which I'm trying to access variables, I get Uncaught TypeError: Cannot read property '...' of undefined . So, let's say: ( function($) { $.fn.main = function() { this.setting = 1; $("#someElement").scroll( function() { console.debug(this.setting); } ); } } )(jQuery); I'm sure it has something to do with timing, but then again, I could be wrong. Should I make a copy of this and make that public? Anyone? Thanks. 回答1: The

keeping track of multiple runs of the same function, part 2

╄→гoц情女王★ 提交于 2019-12-12 02:34:39
问题 This is related to this Anyway what I need is actually something slightly different I need some way of doing this: function run(arg) { this.ran = this.ran || false; if (!this.ran) init; /* code */ this.ran = true; } This works fine, I just want to make sure that this code works even when this in case it was called with call() or apply() Check this out for what I'm talking about, All of the calls after the first one should all be true, no matter the context 回答1: to get full use of closures, i

How do you make the $(this) selector focus on current element?

穿精又带淫゛_ 提交于 2019-12-12 02:22:55
问题 How do you make the $(this) selector focus on current element? In this jsfiddle, I've mounted it only goes in a specified element, so you cant press enter to activate the button you 'are in'. http://jsfiddle.net/mathzarro/gd6Ep/1/ Heres the tricky part: $("button:first").trigger('focus'); Ps. I've said I mounted as an expression! The original coder was Ian, here it is the link.. thanks @Ian! http://jsfiddle.net/Vtn5Y/ 回答1: The real problem was mentioned by HazMat, you were focusing on the

PHP pass in $this to function outside class

别等时光非礼了梦想. 提交于 2019-12-12 01:48:42
问题 Can you pass in a $this variable to use in a function in the "global" space like you can in javascript? I know $this is meant for classes, but just wondering. I'm trying to avoid using the "global" keyword. For example: class Example{ function __construct(){ } function test(){ echo 'something'; } } function outside(){ var_dump($this); $this->test(); } $example = new Example(); call_user_func($example, 'outside', array('of parameters')); //Where I'm passing in an object to be used as $this for

Accessing shared_ptr for this pointer

北战南征 提交于 2019-12-12 01:22:17
问题 Is there a way to get access to the shared_ptr for this: e.g. #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> #include <cassert> class Y: public boost::enable_shared_from_this<Y> { public: void foo(); void bar(); boost::shared_ptr<Y> f() { return shared_from_this(); } }; void Y::foo() { boost::shared_ptr<Y> p(this); boost::shared_ptr<Y> q = p->f(); q->bar(); p.reset(); q.reset(); } void Y::bar() { std::cout << __func__ << std::endl; } int main() { Y y; y.foo(); }

jQuery this.href String Comparison Not Working

只谈情不闲聊 提交于 2019-12-12 01:00:21
问题 I have a simple jQuery script that I'm trying to build upon but I can't get the href string comparison to return true: <a class="test" href="/Services/Cloud-Hosting">Click Me</a>​ My script is as follows: $('.test').click(function() { if ($(this).href == "/Services/Cloud-Hosting") { alert('hi'); } else { alert('no'); } });​ I keep getting the alert of 'no' even thought the hrefs are the same. What am I missing? 回答1: Change: if ($(this).href To: if (this.href Or $(this).attr('href') but former