How, in javascript/jquery, can I change what "this" refers to in an event handling function?

1 Answer

Answer :

By using the call and apply prototype methods of Functions. For example: function example () { console.log(this); } example.call(window); this will be the window variable. example.call(‘a string’); this will be a string. Call and apply are similar to each other, they both call the function and change the scope (the “this” variable) of the function, but where they are different is that apply takes an array of parameters to push onto the function, while call takes extra arguments, for example function testing (var1, var2) { console.log(‘var1: %o var2: %o’, var1, var2); } testing.apply(window, [‘parameter 1’, ‘parameter 2’]); will log “var1: parameter 1 var2: parameter 2”, to do the same with call, would be similar to this: testing.call(window, ‘parameter 1’, ‘parameter 2’); Hope this helps. If you need any more help with this, feel free to just ask.

Related questions

Description : Is JQuery much slower than pure JavaScript ?

Last Answer : answer:You might be correct, there can potentially be more abstraction in JQuery with what you are doing when compared to pure JavaScript. But do note, JavaScript is client side based so it depends on your ... But if you are OK with the delay then keep using JQuery, if not, back to pure JavaScript.

Description : Why use ASP.NET MVC 4 over just HttpHandlers with JQuery Ajax?

Last Answer : answer:I'm not familiar with asp.net, so I'm not sure what it gives you, but a common problem with a single-page app using a lot of jQuery is that it can quickly become unmanageable, especially if ... you other questions, I'm a fan of using only as much as you need and your approach sounds fine.

Description : Dojo vs. JQuery?

Last Answer : I use both at work. I prefer jquery for it’s speed, succinctness, and consistency. However, if you are interested in UI/widgets/etc. I would recommend dojo. In my experience, dijit (the widgets built on top of dojo) are superior to jquery-ui.

Description : How can I become advanced in javascript in 2 months?

Last Answer : Study hard…work hard!!!

Description : How do I simply repeat a line of code 4x in Javascript?

Last Answer : something like a for loop is pretty simple, and gives you a handy counter if needed. for(var i = 0; i < 4; i++) { forward(); }

Description : What's bugging my simple JavaScript toggle script?

Last Answer : answer:The toggle_visibility function specifies (eng, met), not (var1, var2), so it seems not to matter what order of parameters you send it, it's going to read (eng, met) always. function toggle_visibility(eng, ... = block'; } else { e1.style.display = block'; e2.style.display = none'; }

Description : How can I swap two integers without temporary variables using JavaScript code?

Last Answer : answer:This is a trick using the exclusive or operator ^ that was used when computers were short on memory. a = (a ^ b) /* a^b, b */ b = (a ^ b) /* a^b, a */ a = (a ^ b) /* b, a */ You can also do it using subtraction for numeric ... = a - b /* a - b, b */ b = b -a /* a-b, a */ a = b - a /* b, a */

Description : How to inherit a Javascript class?

Last Answer : answer:There is no such thing as classes or object-oriented programming when it come to Javascript. Are you sure you didn't mean Java? Java is a full programming language, developed by Sun. ... used to do thing like change the background color when you hover your mouse over a certain image.

Description : Why should I use JavaScript Library if it may effect the performance of a web site?

Last Answer : You might want to check out some extremely lightweight Javascript libraries such as xui, zepto, or jQuery Mobile. However, 17KB should not be a huge concern on iOS and Android devices. On phones with ... .example.com or mobile.example.com with minimal CSS, small page size, and no Javascript at all.

Description : How to implement a JavaScript code that doing "Set as my Home Page" in Google Chrome?

Last Answer : So, you’re saying the JavaScript code for doing this works in IE and Firefox, but not Chrome? That’s weird. Let me see if I can find a script for you.

Description : Javascript help, please?

Last Answer : any ideas, anyone?

Description : Is Javascript the best way to go when doing a ... photography portfolio site?

Last Answer : Those are all very different technologies that don't really compare to each other. Actionscript is for Flash obviously, which I would not recommend for doing an entire web site in. HTML is the foundation of all ... here. Or you could of course just put them on Flickr and let them manage all of it.

Description : Executing JavaScript in a page retrieved using AJAX?

Last Answer : Would assigning the scripts in page B to an “onload” event handler not work? (I’m new to this, but that seems possible. Forgive my kneejerk response.)

Description : How do I get rid of a javascript warning message from my browser?

Last Answer : Javascript gets mad when you try to access pages on a different domain. So your computer is probably running things as localhost and it is making a call and it says fuck no this is a huge ... . Javascript isn't supposed to load stuff outside of the domain that hosts it. This kinda explains it.

Description : Can I wrap native JavaScript methods with my aspects?

Last Answer : If I am understanding this correctly, you want to do something like authenticate someone, if the auth is returned true then invoke window.open. Really all you need to do is create a JS function ... I believe can be done semi-easily using something like Prototype to handle all of the XHR requests

Description : How can I open a text file in javascript?

Last Answer : Do you mean open a file from the file system? That’s not possible using JavaScript within web browsers due to security restrictions (imagine if that warez site you “stumbled onto” could access your file system…ouch!), but it is possible within some environments, like Apple’s Dashboard.

Description : What's the best editor for PHP/Javascript work?

Last Answer : Vim

Description : Can you use JavaScript to force-replace a particular image in the browser cache?

Last Answer : answer:I'm not sure about doing it in js but if you make it so that the image/stylesheet/javascript file in question has a unique string after the filename like '/images/header.jpg?somerandomstring', when ... the string. That way, whenever you deploy a new version, it'll have a new cache string.

Description : Mystery Javascript snippet?

Last Answer : it looks like a piece of obfuscated code. Because normally JavaScript even in it's most complex is still humanly readable. If it is not obsfucated, then someone must have some really messed up naming ... but for what little I know, it does not look like it would ever work for any browser.

Last Answer : It Basically One Scripting Language . It 's HTML With Work By . To say Anyway , that's it Almost HTML _ Like . It is Absolutely Free . It is That Someone Use To do Can .

Last Answer : JavaScript One Popular Scripting Language , which The web Page Interactivity, Functionality Increase , form Validation , browser Instruction , time, Date Instructions Etc. At work Used Is.

Description : JQuery: wait for other event to finish?

Last Answer : JQuery uses callbacks as an argument to nearly every animation function.. It works by passing a reference to the function you wish to call in the same manner you are passing a new function with the a.click ... #photoViewer').fadeIn(250); }); // close fadeOut() callback }); // close click() callback

Description : What is meant by event drive programming ?

Last Answer : The programming instruction or instruction given through the diagram is called event drive programming. Events such as pressing a key on the corner of a keyboard , clicking a mouse point on a particular control ... in the corner. In this case Command is 'Control' or 'Control' and clicking 'Event'.

Description : What is an event ?

Last Answer : Pressing a key on the keyboard , clicking a mouse pointer on a particular control, etc. is an event.

Description : Match the following mechanisms for interrupting the execution of a process and their uses. i) Interrupt a) Call to an operating system function ii) Trap b) Reaction to an asynchronous external event iii) Supervisor Call c) Handling of a error ... -a, iii-b C) i-b, ii-c, iii-a D) i-a, ii-c, iii-b

Last Answer : C) i-b, ii-c, iii-a

Description : How would you form an if statement in a jQuery function, then call that function using a .click() handler?

Last Answer : Can you show us what your current code looks like? Perhaps you could paste it to http://jsfiddle.net/ or https://gist.github.com/ and give us a link? It’s easier answer if we’re referencing the same things.

Description : Function or application allowing me to connect or disconnect wifi?

Last Answer : Some routers - Belkins and older Ciscos, had an app that would allow you to remotely-control your router from your phone. You didn't say what kind of router you have, so it's hard to guess. You can ... . Bottom line - you can do this easily if you have a wifi-based power switch and an app. example

Description : How do you do an optimal binary search on a two variable function?

Last Answer : You need to clarify your terminology a little bit. Symmetric about the diagonal? Strictly increasing with respect to which ordering of the pairs?

Description : How can I reset the clock in this (C++) timer function?

Last Answer : I don’t know C++ very well but “bool reset = false;” sounds like it shouldn’t be included

Description : Math algorithm/function to convert this number to 40?

Last Answer : answer:Sure the equation is y = 147.5x – 151.75 Where x is the number that goes from 1.3 to 1.7 And Y is the one that goes from 40 to 90. If you want it the other way let me know. Here it is: y = 0.0068x + 1.0288 Enjoy

Description : When to use stored procedure and when function in SQL Server 2008?

Last Answer : There’s no technically right answer here – I think I’d do the SP calling SP.

Description : What is the main function of web design ?

Last Answer : The main function of web design is to create templates.

Description : What is meant by recursive function ?

Last Answer : Answer : When a function in C program automatically calls itself, that function is called recursive function and this process is called recursive. It is very difficult to understand recursive ... be easily solved by recursive functions. Each recursive function has to have a terminating condition.

Description : What are the functions of Printf () and Getch () function ?

Last Answer : The most commonly used function as an output statement in C language is printf (), which can be used to display various types of data (such as int, Char, float, etc.), including strings or strings ... programming . That is, both print () and getch () act as essential functions for C programming .

Description : What is the function of assembler ?

Last Answer : : Assembler's job (1) is to convert the nominal code into machine language. (2) The Assembly converts the address into an address written in machine language. (3) Verify that each instruction ... all the mistakes. (6) To make every instruction in assembly language an instruction in machine language.

Description : How much work does it take to change on a program, from "Monday to Sunday" to whichever 7 days in a row?

Last Answer : It would depend on the individual program.

Description : Making a career change from programming to...?

Last Answer : answer:What about doing training in your field? High-tech companies have in-house training staff and also bring in contractors to train their workers, updating or expanding their skill sets. ... . It seems like there'd be opportunities there for someone with your knowledge and interests.

Description : Do you pick music based on your mood, or to change your mood?

Last Answer : answer:After a long day I love to unwind with some peaceful Norah Jones . There's just something about how her music is put together that resonates with me. In this case I suppose I'm using it to ... to match my mood but I never really thought about it. There are all sorts of reasons to listen.

Description : How feasible is a career change to Android/mobile app development?

Last Answer : No tech background? You will be a mice in a field gathering seeds, while the harvesters are racking in the crop. It's not something to just think about. Like anything, you should have passion for ... just another tool in the field, probably having to work as an intern to a well established company.

Description : Roughly how much do programmers change to create Apps for iPhone/iPad? Does the client get the coding?

Last Answer : My son has developed apps directly for Apple; not through a company. If his experience might be relevant to you @Dog, PM me and I will talk to him.

Description : How do I get clients to pay for change requests?

Last Answer : I'd say that would depend on your original contract you had with the person. Did you have a contract or how was it originally stated on how/what work you'd do for this person? You could state that ... for each additional change $xx.xx price will incure or fix it as it would fit to your busines.

Description : Is there an easy way (or plugin) to change all my eclipse colors to light on dark?

Last Answer : answer:Ok, I think I found a solution to what I was looking for: http://www.romiro.com/archives/7 That will need some tweaking, too. I think eclipse will just always require color tweaking if you don’t like the defaults. Sigh. Resists urge to start investigating other editors.

Description : State true or false. i) Jpanel is a class included in awt package. ii) Anonymous classes are mostly used for event handling. iii) Names of anonymous classes must be unique iv) JOptionPane is an inner class A) i-false ... ) i-false, ii-true, iii-false, iv-false D) i-true, ii-false, iii-false, iv-true

Last Answer : C) i-false, ii-true, iii-false, iv-false

Description : How do I make my jQuery slideshow resize to window?

Last Answer : http://www.buildinternet.com/project/supersized/

Description : Does Jquery Hide content from Search?

Last Answer : It should as long as the content is in the page and not called as a XMLHttpRequest. AKA as AJAX.

Description : How can I write Jquery codes to select a certain element?

Last Answer : Try $(‘div.FatherDiv div.Common:first-child’)

Description : Make a whole blog with jQuery, is it possible?

Last Answer : I would get started with HTML. It seems you fundamentally don’t understand what jQuery is—it’s a library for a language called Javascript which is a client-side scripting language that is used in addition to HTML. I would highly recommend using http://www.w3schools.com/.

Description : Is there a simple flash or jquery code that can do this?

Last Answer : try searching for jquery lightbox I haven’t used a lot of them, but thickbox is pretty good. Here’s another list.

Description : Why is jQuery so much more popular than Prototype and Scriptaculous?

Last Answer : Some reasons off of the top of my head: 1. It is smaller 2. It is faster 3. It is easy to extend (plugins and such) 4. It is true to javascript. Prototype is like a ruby-ized javascript

Description : How do you have Ajax requests fade in with jQuery?

Last Answer : I’m not entirely sure how you would do this in jQuery since I use Prototype, but my usual method is: 1) Load the content into a hidden container 2) onSuccess, fade in the hidden container housing the content I’m sure there’s a similar method in jQuery.