this

Is there any overhead using this-> for accessing a member?

亡梦爱人 提交于 2019-12-30 08:10:16
问题 When accessing a member of some class, I can use e.g.: this->myVar = 10 or I can just write: myVar = 10 I like to use this-> because it explicitly declares that the variable is a member of this class, but does it cause any overhead in comparison to just using the variable name by itself? As an alternative I could maybe add a unique prefix to the vars, such as _TmyVar , but I've been using this-> for a long time so I just wondered. 回答1: There is no overhead. The compiler will generate the

Caching $(this) in jQuery is a best practice?

半世苍凉 提交于 2019-12-30 06:38:28
问题 We all know it's good to cache calls to the DOM, so instead of calling $('#someElement') more times, just save it to a var $someElement and use that. But is it the same when using $(this) inside an event listener for example? Should $(this) be cached? Thank you. 回答1: Each time you call $(this) or $(selector) it is a function call to create a new jQuery object... so if you have already created it once, caching will save calling a function to create the same object again 回答2: If you call $(this

How come $(this) is undefined after ajax call

无人久伴 提交于 2019-12-29 09:09:28
问题 I am doing an ajax request when an anchor tag is clicked with an ID set to href. Btw, this anchor tag is dynamically created. <a href="983" class="commentDeleteLink">delete</a> When the anchor tag is clicked the following code is executed: $('.commentDeleteLink').live('click', function(event) { event.preventDefault(); var result = confirm('Proceed?'); if ( result ) { $.ajax({ url: window.config.AJAX_REQUEST, type: "POST", data: { action : 'DELCOMMENT', comment : $('#commentText').val(),

What is the difference between $(this) and this

不打扰是莪最后的温柔 提交于 2019-12-29 08:39:07
问题 I have the following code $('a').click(function() { var url= this.href; alert(url); }); This works just fine and sure enough the returned result is the url of a tag. However if I change the above code to $('a').click(function() { var url= $(this).href; alert(url); }); The result is undefined. Anyone please help to clear this out for me? I am banging my head for this .... 回答1: $(this) creates a jQuery object which wraps this . The native DOM object has an href attribute, but jQuery does not. $

Groovy: this.metaClass Versus instance.metaClass

我与影子孤独终老i 提交于 2019-12-29 07:36:09
问题 I have encountered below groovy script code in the book . And it generated some strange outputs to me. class Person{ def work(){ println "work()" } def sports=['basketball','football','voleyball'] def methodMissing(String name, args){ if(name in sports){ println "injected ${name} into Person class" Person instance=this println "this.metaClass:\t\t${this.metaClass}" println "instance.metaClass:\t${instance.metaClass}" assert this.metaClass==instance.metaClass }else{ println "no such method:$

smart pointers + “this” considered harmful?

人走茶凉 提交于 2019-12-29 04:02:45
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

smart pointers + “this” considered harmful?

偶尔善良 提交于 2019-12-29 04:02:24
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

Usage of “this” in destructor

扶醉桌前 提交于 2019-12-28 21:49:14
问题 Is it valid to call some function in destructor with this argument? Function does not store pointer, but assume full-functional object. 回答1: this is still valid in the destructor. However, you need bear in mind that virtual functions no longer work properly as you might expect once the object is being destroyed; see e.g. Never Call Virtual Functions during Construction or Destruction. Essentially, the dynamic type of the object is modified as each destructor completes. 回答2: In one word: YES.

“This” within es6 class method [duplicate]

末鹿安然 提交于 2019-12-28 01:55:11
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . For some reason I'm getting weird values for "this" in my es6 class... 'use strict'; class Clicker { constructor(element) { this.count = 0; this.elem = element; this.elem.addEventListener('click', this.click); // logs Clicker { count:0, elem: button#thing} as expected console.log(this); } click() { // logs <button id="thing">...</button> as unexpected... console.log

Does the comma operator influence the execution context in Javascript?

99封情书 提交于 2019-12-27 22:05:11
问题 var a = 1; var b = { a : 2, c : function () { console.log(this.a); } }; b.c(); // logs 2 (b.c)(); // logs 2 (0, b.c)(); // logs 1 The first is understandable, for "this" is pointed to Object "b". But why does the second one log the same result? I thought "this" should be pointed to the global execution context. And the third one, it seems that the comma operator influences the execution context. 回答1: You really have a nice corner case there! My take on it: the first is straightforward. Just a