Ver Mensaje Individual
  #6 (permalink)  
Antiguo 10/07/2014, 12:21
Pantaláimon
 
Fecha de Ingreso: julio-2006
Ubicación: Barcelona
Mensajes: 244
Antigüedad: 17 años, 9 meses
Puntos: 32
Respuesta: Herencia y constructor

Cierto, no lo había pensado así. Ahora mi duda es como se implementa un nuevo namespace. Intentando imitar lo que hace JQuery me ha salido esto:

Código Javascript:
Ver original
  1. function X(f) {
  2.     if( f instanceof Function) {
  3.         return new X.Function(f);
  4.     }
  5. }
  6.  
  7. X.Function = function(f) {
  8.     this.f = f;
  9. }
  10.  
  11. X.Function.prototype.extend = function (base) {
  12.     this.f.prototype = Object.create(base.prototype);
  13.     this.f.prototype.constructor = this.f;
  14. }
  15.  
  16. function B(){
  17.     console.log("B constructor");
  18. }
  19. B.prototype.fbb = function() {}
  20.  
  21. function C(){
  22.     console.log("C constructor");
  23. }
  24.  
  25. X(C).extend(B);
  26. C.prototype.fcc = function() {}
  27.  
  28. var c = new C(); // C constructor : OK
  29. console.log(c instanceof B); // ok
  30. console.log(c instanceof C); // ok
  31. c.constructor(); // C constructor : ok

Primeramente he intentado que la clase X.Function, en vez de tener una propiedad f de la clase Function, fuera una clase extendida de Function. Pero de esta manera no he conseguido hacerlo:
Código Javascript:
Ver original
  1. function X(e){
  2.     if( e instanceof Function) {
  3.         return new X.Function(e);
  4.     }
  5. }
  6.  
  7. X.Function = function() {
  8.     Function.apply(this, arguments);
  9. }
  10. X.Function.prototype = Object.create(Function.prototype);
  11. X.Function.prototype.constructor = X.Function;
  12.  
  13. X.Function.prototype.extend = function (base) {
  14.     this.prototype = Object.create(base.prototype);
  15.     this.prototype.constructor = this;
  16. }

Un saludo!
__________________
github.com/xgbuils | npm/xgbuils