this

Why does using `this` within function give me a “Possible strict violation” in jshint?

邮差的信 提交于 2020-01-03 06:01:07
问题 Say I have a function that I would like reuse as a method on a couple objects in order to add data to those objects. function addToObject(data) { for (var d in data) { if (data.hasOwnProperty(d)) { this[d] = data[d]; } } } myObjOne = { add: addToObject }; myObjTwo = { add: addToObject }; My goal here was to be able to call myObjOne.add(myData) where myData is an object that I would like to add to myObjOne and be able to replicate this functionality on myObjTwo . My issue is that using this

Google Closure Compiler: How to preserve code that caches “this”?

六眼飞鱼酱① 提交于 2020-01-02 08:03:51
问题 Introduction I use Google Closure Compiler frequently for compressing my JavaScript files. Now, it seems to compress my code fairly well. Now, I try to make a habit of storing the this object in a local variable, because this cannot be obfuscated, but a local variable certainly can be obfuscated. However, Google Closure Compiler does not recognize this, and instead removes all instances of the local variable, replacing it with this . Regarding optimization... I am well aware that one should

Best practice in React regarding the context `this`

拟墨画扇 提交于 2020-01-02 07:49:32
问题 Which of the following is the best practice with regard to performance in a React class component: Binding a call back function in the constructor: constructor(props) { /* some code */ this.onChange= this.onChange.bind(this) } render() { return( <div> <input onChange={this.onChange} </div> ); } onChange(event) { /* some code involving 'this' */ } Using an arrow function instead of a normal function: constructor(props) { /* some code */ } render() { return( <div> <input onChange={this.onChange

passing parameters to jquery ui dialog

穿精又带淫゛_ 提交于 2020-01-02 04:07:09
问题 I use .data like this to pass the id of the textbox that calls the dialog $("#<%=txtDirProprio.ClientID%>").focus(function() { $("#<%=dialog.ClientID%>").dialog( "open" ).data("id","#<%=txtDirProprio.ClientID%>"); return false; }); here is the code for the dialog $("#<%=dialog.ClientID%>").dialog({ autoOpen: false, show: "blind", hide: "explode", width: 800, height:200, modal: true, buttons: { "Ajouter": function() { $( this ).dialog( "close" ); StringBuilderDir($( this ).data("id")); },

Can you explain the concept of the this pointer? [closed]

∥☆過路亽.° 提交于 2020-01-02 01:11:07
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . I need to understand this pointer concept, preferably with an example. I am new to C++, so please use simple language, so that I can understand it better. 回答1: this is a pointer to an instance of its class and

this keyword in Java and in PHP

醉酒当歌 提交于 2020-01-01 18:14:44
问题 Today I started work on a small Java app. I have some experience with PHP OOP and mostly the principle is one and the same. Though I thought, that it should apply both ways. But for instance keyword this is used differently as I understand. In Java class Params { public int x; public int y; public Params( int x, int y ) { this.x = x; this.y = y; } public void display() { System.out.println( "x = " + x ); System.out.println( "y = " + y ); } } public class Main { public static void main( String

Is accessing c++ member class through “this->member” faster/slower than implicit call to “member”

坚强是说给别人听的谎言 提交于 2020-01-01 08:37:12
问题 After some searching on our friend google, I could not get a clear view on the following point. I'm used to call class members with this-> . Even if not needed, I find it more explicit as it helps when maintaining some heavy piece of algorithm with loads of vars. As I'm working on a supposed-to-be-optimised algorithm, I was wondering whether using this-> would alter runtime performance or not. Does it ? 回答1: No, the call is exactly the same in both cases. 回答2: It doesn't make any difference.

Settimeout, bind and this

梦想与她 提交于 2020-01-01 06:11:28
问题 Here I have copied code snippet from MDN : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind function LateBloomer() { this.petalCount = Math.ceil(Math.random() * 12) + 1; } // Declare bloom after a delay of 1 second LateBloomer.prototype.bloom = function() { window.setTimeout(this.declare.bind(this), 1000); }; LateBloomer.prototype.declare = function() { console.log('I am a beautiful flower with ' + this.petalCount + ' petals!'); }; var flower = new

What is the context of an anonymous function?

≯℡__Kan透↙ 提交于 2020-01-01 05:23:06
问题 I have code like this: function demo() { this.val=5; function() { this.val=7; }(); } Now when I give execute this code in the firefox or chrome console it gives a syntax error. I don't understand why this is an error because I have read that javascript functions are objects so when I call the anonymous function, inside it this points to function demo and should change the val to 7 , so if I do var x=new demo(); x.val; //should give 7 but when I do this function demo() { this.val=5; var f

Jquery select clicked element within a set of elements with the same classname

a 夏天 提交于 2020-01-01 04:37:09
问题 Maybe you guys can help me out with this, I've been struggling with it for the past 30 minutes. Let's say I have four elements with the same class. <div class="test">hi</div> <div class="test">hey</div> <div class="test">yo</div> <div class="test">What's up</div> How can I select the one that was clicked on? I was able to get it work like this: $('.test').click(function() { $(this).toggleClass("btn-danger btn-success"); }); However, I need it to fire without being clicked on success after an