1

I have written this code, which hasn't been set up for any real function I am just defining a structure

function Finder(d) {
this.ajax = {
    current : null,
    call : function(url,data) {
        //Todo Ajax Code here
    },
    success : function() {
        //Todo: Default Ajax Success Functiality
    },
    error : function(xhr, ajaxOptions, thrownError) {
        //Todo: Default Ajax Failure Functionality
    }
};
d.constructor.extend = function(def){
    for (var k in d) {
        if ( !def.hasOwnProperty(k)) {
            def[k] = d[k];
        }
        return Finder(def);
    }
}
return (d.constructor.prototype = d).constructor;
}

var ModelInput = Finder({
constructor: function() {
    //Todo: Setup functionality
},
registerEvents : function() {
    //Todo: Register Dom Events for ModelInput
}
});



var foo = new ModelInput();

Console output of foo;

Finder.constructor {constructor: function, registerEvents: function}
__proto__: Object
constructor: function () {
registerEvents: function () {
__proto__: Object

My question is why when I instantiate a new ModelInput(); does in not contain the methods and properties of Finder();?

Richard Christensen
  • 2,046
  • 17
  • 28
  • What are you exactly trying to do here? Your code is very confusing.. are you defining Finder as a base class then extending it to ModelInput? – Michael Coxon Jun 06 '14 at 16:17
  • Notice that even if you use prototypical inheritance correctly, as suggested by SergeS, ModelInput will never contain the methods of Finder, but ModelInput's prototype Finder will have the methods. If you are **really** into `extends` look into "pseudo-classical inheritance" – semiomant Jun 06 '14 at 16:32
  • 1
    There are many question regarding inheritance in JS on Stack Overflow, just search for it. You will also find better answers than the ones you got here. – Felix Kling Jun 06 '14 at 16:40
  • 1
    for example: http://stackoverflow.com/questions/18753802 – semiomant Jun 06 '14 at 16:40
  • @semiomant you pointed me in the right direction, much thanks! – Richard Christensen Jun 06 '14 at 19:47

2 Answers2

0
  1. Javascript DOES NOT HAVE INHERITANCE - there is only possibility of adding functions to prototype

  2. Your code also is bit messy , the simpliest way to create prototype with some methods of other prototype is

    function A( ) {
        doSomethingWithThis..
    }
    
    A.prototype = { ... }
    
    function B( ) {
        A.call( this );
    }
    
    B.prototype = new A();
    
    B.prototype.method = function() {
    
    }
    

PS. Once again - Javascript isn't classic OOP language, so consider reworking your concept to use factory methods for example

    function getFinder( ) {
        var thisObj = {};

        thisObj.someFunction = someFunction.bind( thisObj );

        return thisObj;
    }
SergeS
  • 11,533
  • 3
  • 29
  • 35
0

Ok so the only way I have ever been able to do this is with prototyping.. so what I do is this...

http://jsfiddle.net/b2JM8/

// used for extending classes into new classes
Function.prototype.Implement = function (base) {
    var c = Function.prototype.Implement.nonconstructor;
    c.prototype = new base();
    this.prototype = new c();
};
Function.prototype.Implement.nonconstructor = function () { };


var BaseClass = function() {

    var me = this;

    me.BaseMethodOne = function () {
       alert('BaseMethodOne');
    };

    me.BaseMethodTwo= function () {
        alert('BaseMethodTwo');
    };
}


var MyClass = function() {

    var me = this;

    me.MyMethodOne = function () {
       alert('MyMethodOne ');
    };

    me.MyMethodTwo= function () {
        alert('MyMethodTwo');
    };
}

MyClass.Implement(BaseClass);


var myObject = new MyClass();

myObject.BaseMethodOne();
myObject.BaseMethodTwo();
myObject.MyMethodOne();
myObject.MyMethodTwo();
Michael Coxon
  • 5,311
  • 1
  • 24
  • 51