pontoNETpt
A comunidade PontoNetPT está direccionada a todos os programadores que trabalhem com a plataforma .NET.
Javascript: Override a metodos com acesso ao base
Creative|Minds

Syndication

News

Muitos das classes que constuimos quando trabalhamos em object-oriented, permitem, quando declarado, efectuar override ao metodos base quando esta classe é herdada por outra.

Em javascript não é tão facil pois não existe o verdadeiro conceito de herança object-oriented.

Para podermos então criar o override ao metodo mantendo o acesso a base, criei a seguinte extenção ao Object: Object.extend = function(destination, source){ for (var property in source) { var newSource = source[property]; if (destination[property] && (destination[property] instanceof Function) && property != "extend") { var baseSource = destination[property]; newSource = source[property]; var method = newSource; newSource = function() { var previous = this.base; this.base = baseSource; var returnValue = method.apply(this, arguments); this.base = previous; return returnValue; }; // convert the object into the most meaningful primitive value possible // needs to be override for returning correct object newSource.valueOf = function() { return method; }; // convert the object into the most meaningful character string // needs to be override for returning correct object newSource.toString = function() { return String(method); }; } destination[property] = newSource; } return destination;}Object.prototype.extend = function(object){ return Object.extend.apply(this, [this, object]);}

Desta forma podemos criar um objecto base:
myObj = { caller: function() { alert("caller"); } }

Algures mais tarde no código fazer o override ao método e chamar a base:
Object.extend(myObj,{ caller: function() { this.base(); alert("caller extended"); } } );

E por fim chamar o método overrrided:
myObj.caller();

[Via Javascript: Override a metodos com acesso ao base]


Posted 11-9-2006 23:11 por Bruno Figueiredo

Add a Comment

(requerido)  
(opcional)
(requerido)  
Remember Me?
If you can't read this number refresh your screen
Enter the numbers above:  
Powered by Community Server (Commercial Edition), by Telligent Systems