Guest User

Untitled

a guest
Jan 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Javascript - Import and Export class examples</title>
  6. </head>
  7. <body>
  8. <h1>Javascript Import and Export class examples</h1>
  9. <div id="myDiv"></div>
  10. <button onclick="window.updateScreen()">Do it</button>
  11. <script type="module">
  12. import('./main.js').then(module => {
  13. /* This function is now available on the button onclick */
  14. window.updateScreen = module.updateScreen;
  15. });
  16. </script>
  17. </body>
  18. </html>
  19.  
  20. import { Animal } from './animal.js';
  21. import { Human } from './human.js';
  22. import { Dog, Cat } from './pets.js';
  23.  
  24. const petOwner = new Human('Bob', 'Male', '21/03/19');
  25. const pets = [ new Dog('Riley', petOwner), new Cat('Sherry', petOwner) ];
  26.  
  27. petOwner.speak();
  28. pets[1].speak();
  29. pets[0].speak();
  30.  
  31. function updateScreen() {
  32. let elem = document.getElementById('myDiv');
  33. elem.textContent = 'HELLO';
  34. }
  35.  
  36. export { updateScreen };
  37.  
  38. class Animal {
  39. constructor(alive, hr) {
  40. this.isAlive = alive;
  41. this.heartRate = hr;
  42. }
  43. }
  44. export { Animal };
  45.  
  46. import {Animal} from './animal.js';
  47. class Human extends Animal {
  48. constructor(name, sex, dob) {
  49. super(true, 60);
  50. this.name = name;
  51. this.dob = dob;
  52. this.sex = sex;
  53. }
  54. speak() {
  55. console.log('Hello');
  56. }
  57. get age() {
  58. return now-dob;
  59. }
  60. }
  61. export { Human };
  62.  
  63. import {Animal} from './animal.js';
  64.  
  65. class Cat extends Animal {
  66. constructor(name, owner) {
  67. super(true, 200);
  68. this.name = name;
  69. this.owner = owner;
  70. }
  71. speak() {
  72. console.log('Meow');
  73. }
  74. }
  75.  
  76. class Dog extends Animal {
  77. constructor(name, owner) {
  78. super(true, 120);
  79. this.name = name;
  80. this.owner = owner;
  81. }
  82. speak() {
  83. document.getElementById('myDiv').textContent = 'Woof';
  84. }
  85. }
  86.  
  87. export { Cat, Dog };
Add Comment
Please, Sign In to add comment