{"id":1010,"date":"2011-11-30T21:27:57","date_gmt":"2011-12-01T02:27:57","guid":{"rendered":"http:\/\/yuguangzhang.com\/blog\/?p=1010"},"modified":"2013-08-01T22:51:46","modified_gmt":"2013-08-02T02:51:46","slug":"ajax-callbacks-and-function-closures","status":"publish","type":"post","link":"http:\/\/yuguangzhang.com\/blog\/ajax-callbacks-and-function-closures\/","title":{"rendered":"AJAX Callbacks and Function Closures"},"content":{"rendered":"<p>Working with JavaScript closures in an asynchronous context (callbacks) was a memorable experience when I first wrote the code for course links. Now I had a chance to look at it again after restoring the functionality during <a href=\"http:\/\/yuguangzhang.com\/blog\/coursetree-bugfixes\/\">a recent bugfix<\/a>.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1008\" title=\"screenshot.2\" alt=\"\" src=\"http:\/\/yuguangzhang.com\/blog\/wp-content\/uploads\/2011\/11\/screenshot.2.png\" width=\"730\" height=\"547\" srcset=\"http:\/\/yuguangzhang.com\/blog\/wp-content\/uploads\/2011\/11\/screenshot.2.png 730w, http:\/\/yuguangzhang.com\/blog\/wp-content\/uploads\/2011\/11\/screenshot.2-300x224.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><br \/>\nA look under the hood of the fourth nested AJAX call:<\/p>\n<ul>\n<li>postCORS is a function that provides cross browser cross origin POST requests. XDomain request is used for IE, while the usual jQuery.post is used for othe browsers.<\/li>\n<li>because each callback needs to have an associated site dictionary from the previous callback, they need to have their own scope. Otherwise, a global variable would be overwritten each time through the loop in the parent closure.<\/li>\n<li>g simply returns whatever is passed to it. This is one way of creating an independent scope. A related way is to call <em>new<\/em>\u00a0on a function that is a constructor.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Working with JavaScript closures in an asynchronous context (callbacks) was a memorable experience when I first wrote the code for course links. Now I had a chance to look at it again after restoring the functionality during a recent bugfix. A look under the hood of the fourth nested AJAX call: postCORS is a function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[29],"tags":[150,151,133],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-coursetree","tag-ajax","tag-closure","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":0,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"wp:attachment":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}