am_dot_com

SW 2022-04-20

Apr 20th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Meu Tipo</title>
  6. <script src="MeuTipo.js"></script><!-- new data types! -->
  7. </head>
  8. <body>
  9. <script>
  10. //v1 instance creation
  11. var v1 = new MeuTipo();
  12. //observe that, indeed, is a new object
  13. document.write(typeof(v1)); //object
  14. document.write("<br>")
  15. document.write(v1); //?
  16. document.write("<br>")
  17. document.write(v1.constructor.name); //"MeuTipo"
  18.  
  19. document.write("<br>");
  20.  
  21. var v2 = new MeuTipo2();
  22. document.write(typeof(v2)); //object
  23. document.write("<br>")
  24. document.write(v2); //?
  25. document.write("<br>")
  26. document.write(v2.constructor.name); //"MeuTipo2"
  27. </script>
  28. <hr>
  29. <script>
  30. var garfield = new Gato("Garfield");
  31. var pantufas = new Gato("Pantufas");
  32.  
  33. document.write(garfield.enquantoFrase());
  34. document.write(pantufas.enquantoFrase());
  35.  
  36. document.write("<br>");
  37.  
  38. document.write(garfield);
  39. document.write(pantufas);
  40.  
  41. document.write(Gato.miar());
  42. document.write(Gato.miar());
  43. </script>
  44. <hr>
  45. <script>
  46. var g1 = new Gata("Pituxa");
  47. document.write(g1);
  48. document.write(Gata.miar());
  49. </script>
  50. </body>
  51. </html>
  52.  
  53. ****
  54.  
  55. //retro-compatible fashion
  56. //to have custom data types in JS
  57. function MeuTipo(){}
  58.  
  59. function MeuTipo2(){}
  60.  
  61. function Gato(
  62. pNome
  63. ){
  64. //this representa o objeto "corrente"
  65. //no momento da construção/instanciação
  66. //representa o objecto que estiver a ser
  67. //construido
  68. this.mNome = pNome;
  69. }//Gato
  70.  
  71. //Gato.prototype.toString = function(){
  72. //acrescentar o método dynamic "enquantoFrase"
  73. Gato.prototype.enquantoFrase = function(){
  74. var strFrase = "Nome do gato= "+this.mNome+"<br>";
  75. return strFrase;
  76. }//Gato.enquantoFrase
  77.  
  78. //método estático, partilhado por todas as instâncias de Gato
  79. Gato.miar = function(){
  80. return "miau!!!!!!!";
  81. }//miar
  82.  
  83. Gato.prototype.toString = Gato.prototype.enquantoFrase;
  84.  
  85. class Gata{
  86. constructor(pNome) {
  87. this.mNome = pNome
  88. }//constructor
  89.  
  90. toString(){
  91. var strFrase = "Nome da gata= "+this.mNome+"<br>";
  92. return strFrase;
  93. }//toString
  94.  
  95. static miar(){
  96. return "miau!!!!!!!";
  97. }//miar
  98. }//class Gata
Advertisement
Add Comment
Please, Sign In to add comment