Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Meu Tipo</title>
- <script src="MeuTipo.js"></script><!-- new data types! -->
- </head>
- <body>
- <script>
- //v1 instance creation
- var v1 = new MeuTipo();
- //observe that, indeed, is a new object
- document.write(typeof(v1)); //object
- document.write("<br>")
- document.write(v1); //?
- document.write("<br>")
- document.write(v1.constructor.name); //"MeuTipo"
- document.write("<br>");
- var v2 = new MeuTipo2();
- document.write(typeof(v2)); //object
- document.write("<br>")
- document.write(v2); //?
- document.write("<br>")
- document.write(v2.constructor.name); //"MeuTipo2"
- </script>
- <hr>
- <script>
- var garfield = new Gato("Garfield");
- var pantufas = new Gato("Pantufas");
- document.write(garfield.enquantoFrase());
- document.write(pantufas.enquantoFrase());
- document.write("<br>");
- document.write(garfield);
- document.write(pantufas);
- document.write(Gato.miar());
- document.write(Gato.miar());
- </script>
- <hr>
- <script>
- var g1 = new Gata("Pituxa");
- document.write(g1);
- document.write(Gata.miar());
- </script>
- </body>
- </html>
- ****
- //retro-compatible fashion
- //to have custom data types in JS
- function MeuTipo(){}
- function MeuTipo2(){}
- function Gato(
- pNome
- ){
- //this representa o objeto "corrente"
- //no momento da construção/instanciação
- //representa o objecto que estiver a ser
- //construido
- this.mNome = pNome;
- }//Gato
- //Gato.prototype.toString = function(){
- //acrescentar o método dynamic "enquantoFrase"
- Gato.prototype.enquantoFrase = function(){
- var strFrase = "Nome do gato= "+this.mNome+"<br>";
- return strFrase;
- }//Gato.enquantoFrase
- //método estático, partilhado por todas as instâncias de Gato
- Gato.miar = function(){
- return "miau!!!!!!!";
- }//miar
- Gato.prototype.toString = Gato.prototype.enquantoFrase;
- class Gata{
- constructor(pNome) {
- this.mNome = pNome
- }//constructor
- toString(){
- var strFrase = "Nome da gata= "+this.mNome+"<br>";
- return strFrase;
- }//toString
- static miar(){
- return "miau!!!!!!!";
- }//miar
- }//class Gata
Advertisement
Add Comment
Please, Sign In to add comment