Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created May 25, 2012 02:08
Show Gist options
  • Save rbarros/2785381 to your computer and use it in GitHub Desktop.
Save rbarros/2785381 to your computer and use it in GitHub Desktop.
Exemplo de prototype apply e zInherit
/* Exemplos classes prototype
* Este é o JavaScript Scratchpad.
*
* Forneça algum JavaScript, clique com o botão direito ou selecione o menu Execução:
* 1. Executar o texto selecionado,
* 2. Inspecionar: abra o resultado em uma janela “Inspecionar objeto”
* 3. Visualizar: insira o resultado em um comentário depois da selecão.
*/
/*------------------------------------------------------------------------------
* JavaScript zInherit Library
* Version 1.0
* by Nicholas C. Zakas, http://www.nczonline.net/
* Copyright (c) 2004-2005 Nicholas C. Zakas. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*------------------------------------------------------------------------------
*/
/**
* Inherits properties and methods from the given class.
* @scope public
* @param fnClass The constructor function to inherit from.
*/
Object.prototype.inheritFrom = function (fnClass /*: Function */) /*:void*/ {
/**
* Inherits all classes going up the inheritance chain recursively.
* @param fnClass The class to inherit from.
* @param arrClasses The array of classes to build up.
* @scope private
*/
function inheritClasses(fnClass /*:Function*/,
arrClasses /*:Array*/) /*:void*/ {
arrClasses.push(fnClass);
if (typeof fnClass.__superclasses__ == "object") {
for (var i=0; i < fnClass.__superclasses__.length; i++){
inheritClasses(fnClass.__superclasses__[i], arrClasses);
}
}
}
if (typeof this.constructor.__superclasses__ == "undefined") {
this.constructor.__superclasses__ = new Array();
}
inheritClasses(fnClass, this.constructor.__superclasses__);
for (prop in fnClass.prototype) {
if (typeof fnClass.prototype[prop] == "function") {
this[prop] = fnClass.prototype[prop];
}
}
};
/**
* Determines if the given object is an instance of a given class.
* This method is necessary because using {@link #inheritFrom} renders
* the JavaScript <code>instanceof</code> operator useless.
* @param fnClass The constructor function to test.
* @return True if the object is an instance of the class, false if not.
* @scope public
*/
Object.prototype.instanceOf = function (fnClass /*:Function*/) /*: boolean */ {
if (this.constructor == fnClass) {
return true;
} else if (typeof this.constructor.__superclasses__ == "object") {
for (var i=0; i < this.constructor.__superclasses__.length; i++) {
if (this.constructor.__superclasses__[i] == fnClass) {
return true;
}
}
return false;
} else {
return false;
}
};
function A(sColor){
this.color = sColor;
this.sayColor = function(){
console.info(this.color);
};
}
function B(sColor, sName){
this.n = A;
delete this.n(sColor);
this.name = sName;
this.sayName = function(){
console.info(this.name);
};
}
function nome(sPrefixo,sSufixo){
alert(sPrefixo + this.parametro_interno + sSufixo);
};
/*
var obj = new Object();
obj.parametro_interno = 'teste';
nome.call(obj, 'O ',' é o mais elegante.');
nome.apply(obj,new Array('O ',' e o mais elegante. '));
*/
function A(sNome) {
this.nome = sNome;
this.digaNome = function () {
alert(this.nome);
};
}
function B(sNome, sName) {
A.apply(this, arguments);
this.sobrenome = sName;
this.digaSobrenome = function () {
alert(this.sobrenome);
};
}
/*
var objA = new A('martins');
var objB = new B('Christiano', 'milfont');
objA.digaNome(); // 'martins'
objB.digaNome(); // 'Christiano'
objB.digaSobrenome(); // 'milfont'
*/
function VeiculoCaract(rodas,velocidade,cor){
this.rodas = rodas;
this.velocidade = velocidade;
this.cor;
this.getVelocidade = function(){
console.info(this.velocidade);
}
}
function Veiculo(rodas,velocidade,cor){
VeiculoCaract.apply(this, arguments);
this.liga = function(){
console.info('Carro lidado');
}
this.acelera = function(){
console.info('Carro movendo');
}
};
var carro = new Veiculo(4,250,'blue');
var moto = new Veiculo(2,150,'black');
carro.getVelocidade(); // saida: 250
moto.getVelocidade(); // saida: 150
Veiculo.prototype.inheritFrom(VeiculoCaract);
Veiculo.prototype.portas;
Veiculo.prototype.getPortas = function(){
console.info(this.portas);
}
carro.portas = 4;
carro.getPortas(); // saida: 4
moto.portas = 0;
moto.getPortas();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment