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.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; | |
} | |
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(){ | |
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(){ | |
//... 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
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
// 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
// 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
function minhaFuncao(){ | |
console.log(arguments.length); | |
} | |
minhaFuncao(0, 1, 2, 3, 4, 5, 6) | |
//=> 7 |
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
MyObj.prototype.Inc = function $Inc(){ | |
console.log(this._num); | |
this._num++; | |
if(this._num >= 10) | |
return; | |
// chamada recursiva da mesma função. Se chamássemos simplesmente utilizando arguments.callee() | |
// causaríamos um estouro de pilha por que nunca teríamos o estado do campo _num preservado, | |
// já que o contexto da função estaria sempre mudando a cada chamada. Para manter este contexto | |
// ativo estamos utilizando a função apply do objeto Function qye nos permite passar o contexto para o qual | |
// queremos que a função execute. O mesmo ocorreria caso quiséssemos utilizar o alias $Inc. Escreveríamos |