How do I pass the this context to a function?

Javascripts .call() and .apply() methods allow you to set the context for a function. Now you can call: Which would alert FOO. The other way around, passing obj_b would alert BAR!!. The difference between .call() and .apply() is that .call() takes a comma separated list if you’re passing arguments to your function and .apply() needs … Read more

Scope Chain in Javascript

To understand the scope chain you must know how closures work. A closure is formed when you nest functions, inner functions can refer to the variables present in their outer enclosing functions even after their parent functions have already executed. JavaScript resolves identifiers within a particular context by traversing up the scope chain, moving from … Read more

Difference between ?:, ?! and ?=

The difference between ?= and ?! is that the former requires the given expression to match and the latter requires it to not match. For example a(?=b) will match the “a” in “ab”, but not the “a” in “ac”. Whereas a(?!b) will match the “a” in “ac”, but not the “a” in “ab”. The difference … Read more

Clearing my form inputs after submission

Your form is being submitted already as your button is type submit. Which in most browsers would result in a form submission and loading of the server response rather than executing javascript on the page. Change the type of the submit button to a button. Also, as this button is given the id submit, it … Read more

Array Mapping in AngularJs

Make a map of your data that looks like this: You can do this by looping over your tags and adding a new property to an object. reduce lets you do this without creating any extra variables. Then, you can select names from your newly created object using the [] notation: tagMap[1] returns “pyhton”. Expand … Read more

jQuery replace one class with another

To do this efficiently using jQuery, you can chain it like so: For simplicities sake, you can also do it step by step like so (note assigning the jquery object to a var isnt necessary, but it feels safer in case you accidentally remove the class you’re targeting before adding the new class and are … Read more

command not found: jest

Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./node_modules/.bin/jest –updateSnapshot. Since you already have jest as a scripts command in your package.json you can also run it with npm test — –updateSnapshot. npm automatically adds ./node_modules/.bin to your path. update: Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it … Read more