Guest User

Untitled

a guest
Feb 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. // Modules should be Immediately-Invoked-Function-Expressions (IIFE) to allow for private scopes
  2. // return an object instead of a function
  3. var ChangeAttribute = (function() {
  4. var content = 'content' // private variable
  5.  
  6. var changeHTML = function() {
  7. var element = document.getElementById('attribute-to-change');
  8. element.innerHTML = content;
  9. }
  10.  
  11. return { // here begin the magic ^^, when you return function.
  12. callChangeHTML: function() { // became public variable, but onlly if you call callChangeHTML!
  13. changeHTML();
  14. console.log(content);
  15. }
  16. };
  17.  
  18. })();
  19.  
  20. ChangeAttribute.callChangeHTML(); // Outputs: 'content'
  21. console.log(ChangeAttribute.content); // undefined
Add Comment
Please, Sign In to add comment