{"id":7552,"date":"2014-11-12T13:17:17","date_gmt":"2014-11-12T13:17:17","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/does-each-call-to-sp-clientcontext-get_current-return-the-same-instance\/"},"modified":"2016-07-28T10:55:50","modified_gmt":"2016-07-28T10:55:50","slug":"does-each-call-to-sp-clientcontext-get_current-return-the-same-instance","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/does-each-call-to-sp-clientcontext-get_current-return-the-same-instance\/","title":{"rendered":"Does Each Call to SP.ClientContext.get_current() Return the Same Instance?"},"content":{"rendered":"<p>I was trying to figure this out for some common code that we are putting together so I ran the following test:<\/p>\n<p><code>function TestReferences() {     <br \/>&#160;&#160;&#160; var contextA = SP.ClientContext.get_current();      <br \/>&#160;&#160;&#160; var contextB = SP.ClientContext.get_current();      <\/p>\n<p>&#160;&#160;&#160; contextA.test = 5;      <br \/>&#160;&#160;&#160; alert(contextB.test);&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Displays 5      <br \/><\/code><code>&#160;&#160;&#160; alert(contextA == contextB);&#160; \/\/Results in True     <br \/>&#160;&#160;&#160; alert(contextA === contextB); \/\/Results in True      <br \/>} <\/code><\/p>\n<p>The result was that each call to SP.ClientContext.get_current() returns the same instance each time, which is what I was hoping based on the name of the function.&#160; This is also true for the get_web() call off the client context object.&#160; If you need to return a different context then you can still spin up your own, but if you want to reuse the same context between pieces of code then SP.ClientContext.get_current() allows you to do that without having create your own variable to share out the instance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to figure this out for some common code that we are putting together so I ran the following test: function TestReferences() { &#160;&#160;&#160; var contextA = SP.ClientContext.get_current(); &#160;&#160;&#160; var contextB = SP.ClientContext.get_current(); &#160;&#160;&#160; contextA.test = 5; &#160;&#160;&#160; alert(contextB.test);&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Displays 5 &#160;&#160;&#160; alert(contextA == contextB);&#160; \/\/Results in True &#160;&#160;&#160; alert(contextA === contextB); \/\/Results&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-7552","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=7552"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7552\/revisions"}],"predecessor-version":[{"id":42438,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7552\/revisions\/42438"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=7552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=7552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=7552"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=7552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}