This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // definindo a função que será utilizada como contrutor do nosso objeto. | |
| var Carro = function(marca){ | |
| this.marca = marca; | |
| } | |
| // criamos um objeto carro utilizando o operador new | |
| var camaro = new Carro('camaro'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // checando a instancia do objeto criado | |
| alert(camaro instanceof Carro); | |
| //=> true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Animal = function(){ | |
| //... | |
| } | |
| var dog = new Animal; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| //... aqui iremos escrever o código da nossa classe! | |
| })(); // <- repare que esta função é executada imediatamente após criada! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| } | |
| })(); | |
| var camaro = new Carro('camaro'); | |
| //=> Error: A função carro não existe no contexto global |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(exports){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| } | |
| exports.Carro = Carro; | |
| })(this); | |
| var camaro = new Carro('camaro'); | |
| //=> Instância criada com sucesso. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(exports){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| } | |
| Carro.prototype.andar = function(km){ | |
| console.log('andando a ' + km + 'km/h'); | |
| } | |
| exports.Carro = Carro; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(exports){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| } | |
| // campo público! | |
| Carro.prototype.Km = null; | |
| Carro.prototype.andar = function(){ | |
| console.log('andando a ' + this.Km + 'km/h'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(exports){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| } | |
| Carro.prototype.Km = 0; | |
| Carro.prototype.andar = function(){ | |
| console.log(textoFormatado.call(this)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(exports){ | |
| function Carro(marca){ | |
| this.marca = marca; | |
| // este é um campo privado. | |
| // campos privados precisam ser definidos dentro do construtor da classe. | |
| _km = 0; | |
| } | |
| Carro.prototype.getKm = function(){ |