pass argument to the parent class in javascript modular pattern -
i have made 2 classes, person - parent class , student class - child class. trying implement inheritance. trying implement in modular pattern. not getting properly. code block please check below.
person.js
var person = function() { var details, constructor, getdetailsbyattribute, setdetailsbyattribute, setdetails, getdetails, cleardetails; cleardetails = function() { details = { uid : '', name : '', email : '' }; }; getdetailsbyattribute = function(attr) { return details[attr]; }; setdetailsbyattribute = function(key, value) { if(details.hasownproperty(key)) { details[key] = value; }else { console.log('invalid key'); } }; setdetails = function(param) { cleardetails(); for(var attr in param) { if(details.hasownproperty(attr)) { details[attr] = param[attr]; } } }; getdetails = function() { return details; }; /** * during object creation; */ if(arguments.length >= 1) { //cleardetails(); setdetails(arguments[0]); } return { getdetailsbyattribute : function(attr) { return getdetailsbyattribute(attr); }, setdetailsbyattribute : function(key, value) { setdetailsbyattribute(key, value); }, setdetails : function(params) { setdetails(params); }, getdetails : function() { return getdetails(); }, cleardetails : function() { cleardetails(); } }; };
student.js
var student = function() { person.call(this,arguments); }; student.prototype = new person();
index.html
<html> <head> </head> <body> <script type="text/javascript" src="modules/person.js"></script> <script type="text/javascript" src="modules/student.js"></script> <script type="text/javascript"> var person1, person2, person3, student1; person1 = new person({ uid : 5225, name : 'jaison', email : 'jaison@jaison.com' }); person2 = new person({ uid : 5222, name : 'jatin', email : 'jatin@jatin.com' }); person3 = new person(); person3.setdetails({ uid : 5221, name : 'sarath', email : 'sarath@sarath.com' }); person3.setdetailsbyattribute('name', 'sarath'); student1 = new student({ uid : 5221, name : 'sachin', email : 'sachin@sachin.com' }); console.log('person 1 : ',person1.getdetails()); console.log('person 2 : ',person2.getdetails()); console.log('person 3 : ',person3.getdetails()); console.log('student 1 : ',student1.getdetails()); //console.log(student1.get); </script> </body> </html>
try this:
var student = function() { return person.apply(this, arguments); };
without
student.prototype = new person();
update: realize can use this:
var student = function() { this.setdetails.apply(this, arguments); }; student.prototype = new person();
so closure inside prototype, , setdetails access it.
in code when this:
student.prototype = new person();
you create closure , when call this
var student = function() { person.call(this,arguments); };
you create closure (and should not call apply, not work). execute constructor function , (if apply will) apply arguments different closure, , functions returned person detail inside closure discard. methods of student taken prototype not constructor.
Comments
Post a Comment