0

i am trying to create a abstract class (mimicking oops) in js using below declaration. If i use prototype keyword for every method below it fails (see case-1 below) however when i use without prototype keyword it works not sure am i declaring it correclty.. however same declaration works when js file is declared to the main page but fails when invoke from dialog box.. i think declaration is issue

if(nsp === undefined) var nsp  = {}; // nsp is the namespace

nsp.abstractCls=  function(){} ; //abstractCls is the abstract function & subclass function will extend perfAction methods (overriden methods)
nsp.abstractCls.prototype ={};

//case-1 fails , throws error is not a function (prototype keyword is present)
nsp.abstractCls.prototype.perfActiOn = function(){
    return "success";
} ;

//case-2 this works (no prototype keyword)
nsp.abstractCls.perfActiOn = function(){
    return "success";
} ;

Statment nsp.abstractTbl.prototype.perfTable wroks but not sure is it correct approach to have prototype keyword to invoke every function..

user593029
  • 511
  • 7
  • 18

1 Answers1

0

Try taking out the line where you declare the prototype for that class to a new object.

Jordan Denison
  • 2,649
  • 14
  • 14
  • tblns.abstractTbl.prototype = new Object(); is not working & tblns.abstractTbl = new Object(); is throwing error prototype is undefined for method perfAction – user593029 Oct 02 '12 at 14:30
  • Yes, remove that line entirely : ) – Jordan Denison Oct 02 '12 at 14:32
  • Nope it does not work.. it throws the same error.. also i am not sure it is right thing to do as if we remove prototype on every method than the function becomes common to every object which i do not want because it may be true for abstract class but i am having the same issues with subclass.. one more thing when i use namespace that is where it issue start coming ..if i use it directly without nsp it works!!.. it works charm as given in this link & my code is similar to that http://phrogz.net/JS/classes/OOPinJS2.html – user593029 Oct 02 '12 at 14:36
  • Lastly, stmt => tblns.abstractTbl.prototype.perfTable, works charm but still the bigger question is how?? & what is causing it to work & million $ question is correct approach – user593029 Oct 02 '12 at 14:50
  • I would have a look at this answer: http://stackoverflow.com/questions/4082482/possible-to-have-abstract-class-in-js – Jordan Denison Oct 02 '12 at 14:53
  • if anyone else experience this is issue it is bcos the namespace is not recognize at window level & to resolve this issue is to have a generic method refer fiddle http://jsfiddle.net/brijeshdkac/Uspzf/ & then declare all the methods with prototype that way namespace will be resolve correctly.. then you can easily call all the prototype methods without explicity writing prototype – user593029 Oct 02 '12 at 15:40