Basic example of using .ajax() with JSONP?

There is even easier way how to work with JSONP using jQuery

<span class="pln">$</span><span class="pun">.</span><span class="pln">getJSON</span><span class="pun">(</span><span class="str">"http://example.com/something.json?callback=?"</span><span class="pun">,</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">result</span><span class="pun">){</span>
   <span class="com">//response data are now in the result variable</span><span class="pln">
   alert</span><span class="pun">(</span><span class="pln">result</span><span class="pun">);</span>
<span class="pun">});</span>

 

Javascript programming styles

My current state of thinking:

prototypes make sense if I have many of a thing that has its own state and I want to interact with them individually.

Here’s a terrible off-the-cuff example: a group of addresses, where each needs to be able to display, edit, save, delete, validate, lookup postal code, set/change the nickname. I will interact with just one at a time, but they all do the same thing.

functional makes sense if I am acting on a set of items.

A terrible off-the-cuff example: A set of animated snowflakes on a page. While they could be instances, they all use the same behavior and are probably updated as a group, so they can be an array of data rather than instances. You can perform logic against the group, like filtering to see if any of them have arrived at a surface and should stop animating.

composition makes sense for grouping actions/behaviors.

Convoluted off-the-cuff example: I’m simulating a network, and each device can be a client, a server, a router, a firewall, or some combination of those. I can use composition to assemble the functionality of a particular device when I create or modify it.

mixing these can make sense, too. You can use composition to build prototypes to give you the flexibility to assemble actions and behaviors but still have the reuse of a prototype. You can use functional programming to operate over a set of instanced objects when it makes sense.

Don’t get trapped in a paradigm.

 

 

How to return the response from an asynchronous call?

I tried to return the value from the success callback as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.

Synchronous

Imagine you make a phone call to a friend and ask him to look something up for you. Although it might take a while, you wait on the phone and stare into space, until your friend gives you the answer you needed.

Asynchronous

You call your friend again for the same reason. But this time you tell him that you are in a hurry and he should call you back on your mobile phone. You hang up, leave the house and do whatever you planned to do. Once your friend calls you back, you are dealing with the information he gave to you.

Solution(s)

Embrace the asynchronous nature of JavaScript! While certain asynchronous operations provide synchronous counterparts (so does “Ajax”), it’s generally discouraged to use them, especially in a browser context.