How to create an object with two or more instances in Javascript?

Asked

Viewed 220 times

2

In Javascript I know that it is possible to create an instance of a class using 'new'

new Classe

And how to create an object that is instance of several classes? For example

new Classe, OutraClasse...

I believe this may be possible because it warns true:

var el = document.createElement('a');
alert(el instanceof HTMLElement && el instanceof HTMLAnchorElement);

Moreover, el has more than 2 instances...

  • 1

    The text of the question is confused. I believe you want to know if it is possible to create an object that is 2 class instance. Strictly speaking this is possible as long as one class is derived from the other, as is the case with Htmlanchorelement and Htmlelement. But in the general case this is not possible in object-oriented programming, an object is always an instance of a single class.

  • @Joséx. Class derived from the other? How could I derive one class from the other?

  • 2

    see reply from @Sergio

2 answers

3

There is only one instance created. An instance can have more than one type, as long as there is hierarchy between them. So el is the type HTMLAnchorElement which in turn was inherited from HTMLElement, bearing this object satifaz at least these two types, after all everything that exists in the base "class" is available in the derived "class".

Note that the normal in Javascript is to use prototypes and not classes. see more in There is a class in Javascript?, Javascript is an Object-Oriented language? and What is a Javascript Prototype?.

3


Note the following example:

class Animal {
    mostrarNome() {
        console.log('Eu sou um animal qualquer...');
    }
}

class Humano extends Animal {
    mostrarNome() {
        console.log('Eu sou um ser humano...');
    }
}

class Pessoa extends Humano {
    constructor(nome) {
        super();
        this.nome = nome;
    }
    mostrarNome() {
        console.log('Eu sou um ser humano... de nome ' + this.nome);
    }
}

var vaca = new Animal();
var sergio = new Pessoa('Sérgio');
vaca.mostrarNome(); // Eu sou um animal qualquer...
sergio.mostrarNome(); // Eu sou um ser humano... de nome Sérgio

console.log(vaca instanceof Animal, vaca instanceof Humano, vaca instanceof Pessoa); // true false false
console.log(sergio instanceof Animal, sergio instanceof Humano, sergio instanceof Pessoa); // true, true, true

What is happening here is that you have classes that you inherit from others. All classes that are descended from others always give true when you check if they are an instance of the original. This is called inheritance and what happens is that the class from which it is inherited is written in the prototype of the new class. There are comprehensive answers to this, as @Maniero indicated.

I created this example to make you realize that what you want is possible. Simply using extends in ES2015 syntax, or by copying the prototype by hand with:

var NovaClasse = Object.create(ClasseAntiga);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.