jquery - Why does this Javascript object not go out of scope after $(document).ready? -
I have some javascript which manages some dome elements The problem is, I do not understand that Why This works, which is never a good thing, I'm trying to learn more about Object Oriented Javascript and Javascript best practices, so the organization looks a bit awkward.
In fact, I wrap two methods that manipulate the DOM inside a CSContent object. I make an example of that object,
content
in $ (document). Bind some events to work in
and content
. However, I am confused as to how these tasks can still be said after $ (document). This does not mean that
content
has gone out of the scope, and its work is not available? Anyway, here's the code:
function CSContent () {var tweetTextArea = document.getElementById ('cscontent-tweet'), tweetTextElement = document.getElementById ('edit-cscontent-cs-content -TV '), charCountElement = document.getElementById (' cscontent-Tweet-charactercount '); This.toggleTweetTextarea = function () {$ (tweetTextArea) .slideToggle (); }; This.updateTweetCharacterCount = function () {var numOfCharsLeft = 140 - tweetTextElement.value.length; If (numOfCharsLeft & lt; 0) {$ (charCountElement) .addClass ('cscontent-negative-chars-left'); } And {$ (charCountElement) .removeClass ('cscontent-negative-chars-left'); } CharCountElement.innerHTML = '' + numOfCharsLeft + 'characters remaining.'; }; } $ (Document) .ready (function () {var content = new CSContent (); // If the Twitter box is unchecked, hide the text area if ($ ('(edit- cscontent-cs-content- Twitter: check '). Val () === Undefined) {$ (' # cscontent-tweet ') Hide ();} $ (' # edit-cscontent-cs-content-twitter '). ToggleTweetTextarea; // seems useless, but when we delete characters, press the keyboard and keys to fix some weird corruption behaviors. $ ('# Edit-cscontent-cs-content-tweet') .PressPress (content.updateTweetCharacterCount ); $ ('# Edit-cscontent-cs-content-tweet'). ZIP (content.updateTweetCharacterCount); content.updateTweetCharacterCount ();});
I I'm surprised that event handlers like
$ ('# edit-cscontent-cscontent-twitter') change (content.toggleTweetTextarea).
Work?
OK that you do not pass content
as event handler, but the function present in content.toggleTweetTextarea
. And this reference will still exist content
no longer exists. There is nothing special about it. You have assigned an object in another variable until it is present in at least one reference to an object, object garbage will not be collected.
Now you can ask that those functions do not have access yet tweetTextArea
? This is actually one stop when the function is created through new CSContent ()
, then the activation reference of this function will be given to internal functions CSContent.toggleTweetTextarea
and CSContent is added to the scope of UpdateTweetCharacterCount
. So even if you do not have any reference to content
, then the scope of this function is still included in the scope of other functions.
You will not be able to use the object contained in content
and after the code has finished, it actually gets out of the scope. is.
Comments
Post a Comment