Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. Object.defineProperty(<some parameters...>)
  2.  
  3. // Define the extend function
  4. Object.defineProperty(Element.prototype, 'extend', { value: function(namespace, object) {
  5. // Create the namespace and make sure on invocation the Element is bound
  6. Object.defineProperty(Element.prototype, namespace, { value: function() {
  7. var extended = Element.prototype[namespace].extended;
  8. for (var e in extended) {
  9. extended[e] = extended[e].bind(this);
  10. }
  11. return extended;
  12. }});
  13.  
  14. this[namespace].extended = object;
  15. return this;
  16. }});
  17.  
  18. // Extend the Element prototype with an attr function in
  19. // the namespace 'namespace' or ofcourse whatever function
  20. Element.prototype.extend('namespace',{
  21. attr: function(name, value) {
  22. if(arguments.length === 1) {
  23. return this.getAttribute(name);
  24. } else {
  25. this.setAttribute(name, value);
  26. return this;
  27. }
  28. }
  29. });
  30.  
  31. document.querySelector('.a-css-class').namespace().attr('class')
  32. // returns 'a-css-class'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement