Posted By

rickygri on 07/20/14


Tagged

javascript object prototype objective


Versions (?)

Javascript object template


 / Published in: JavaScript
 

URL: http://www.adobe.com/devnet/html5/articles/javascript-object-creation.html

A template for a javascript object with a object.create shiv to condense into object prototypes

  1. $(function() {
  2.  
  3. /*** Template ***/
  4.  
  5. /* Object.create shiv */
  6. if(typeof Object.create !== "function") {
  7. Object.create = function (o) {
  8. function F() {}
  9. F.prototype = o;
  10. return new F();
  11. };
  12. }
  13.  
  14. var myObject = {
  15. option1: null,
  16. option2: null,
  17. option3: 0,
  18. /*
  19. * All styles will be added to the DOM, so go nuts.
  20. * 3D transforms are added to transitions
  21. */
  22. styles: {
  23. "slides": '#feature-images ul li,
  24. #feature-caption ul li
  25. {
  26. -webkit-transform: translateZ(0);
  27. -moz-transform: translateZ(0);
  28. -ms-transform: translateZ(0);
  29. -o-transform: translateZ(0);
  30. transform: translateZ(0);
  31. }',
  32. },
  33. init: function() {
  34.  
  35. this.reset();
  36.  
  37. for (var prop in this.styles) {
  38. if (this.styles.hasOwnProperty(prop)) {
  39.  
  40. // Append all properties in styles
  41. // object as CSS node
  42. this.appendStyle(this.styles[prop]);
  43.  
  44. }
  45. }
  46. },
  47. /*
  48. * This function adds styles to the DOM
  49. * as a CSS node
  50. */
  51. appendStyle: function(style) {
  52. var css = document.createElement('style');
  53. css.type = 'text/css';
  54. if (css.styleSheet) css.styleSheet.cssText = style;
  55. else css.appendChild(document.createTextNode(style));
  56. document.getElementsByTagName("head")[0].appendChild(css);
  57. },
  58. reset: function() {
  59.  
  60. }
  61. };
  62.  
  63. var xyz = Object.create(myObject);
  64. xyz.init();
  65. // feature.setActive(2);
  66.  
  67. });

Report this snippet  

You need to login to post a comment.