javascript - how to implement parasitic inheritance to avoid nesting -


enter image description here

i follow inheritance structure shown above. create engineer using syntax:

var mark = new employee(id).workerbee(project).engineer(); 

to achieve syntax, have create nested object following parasitic inheritance pattern so:

    function employee(id) {       this.id = id;        this.workerbee = function(project) {         this.project = project;          this.engineer = function() {           ...           return this;         };          return this;       };     } 

to avoid deep layers of nesting, trying rewrite using prototypes. how can rewrite code achieve same goal above ?

      function employee(id) {          //variables          this.id = id          this.name = "";          this.dept = "general";            //methods          this.getid = function() {            return this.id          }        }      employee.prototype.workerbee = workerbee;        function workerbee(project) {        //variables        this.projectname = project        this.projects = [];        //methods        this.getprojectname = function() {          return this.projectname        }        return      }      workerbee.prototype.engineer = engineer        function engineer() {        //variables        this.dept = "engineering";        this.machine = "";        //methods        this.getdept = function() {          return this.dept        }        return      }        var mark = new employee("5").workerbee("secret project").engineer();      console.log(mark.getid()) //should print "5"      console.log(mark.getprojectname()) //should print "secret project"      console.log(mark.getdept()) //should print engineering

update:

ok, understand partially. reason why want this? want shortcut creating multiple instances using multiple statements?

should instance of c returned a().b().c() different 1 created standard new c()?

if want chain constructors, can add context in defined (most global object) prototype chain of created entities. should able that:

var = function () {}; a.prototype = object.create(this); 

what not eliminate though need new keyword instantiation. need new (new (new a()).b()).c(). can't think of different approach having helper function create constructors not require new keyword:

var define = function (init) {   var constructor = function () {     if (!(this instanceof constructor)) {       return new constructor();     }     if (init) {       init.apply(this, array.prototype.slice.call(arguments));     }   };   constructor.prototype = object.create(this);   return constructor; }; 

the usage is:

var = define(function (x, y) {   this.x = x;   this.y = y; });  var a1 = new a(1, 2); // a1 instanceof === true // a1.x === 1 // a1.y === 2  var a2 = a(1, 2); // a2 instanceof === true // a2.x === 1 // a2.y === 2 

if have constructors a, b , c, can use following notations interchangeably:

var = new a(); var b = new b(); var c = new c();  var = a(); var b = b(); var c = c();  var b = a().b(); var c = a().c();  var = b().c().a(); 

in case of a().b().c(), not have access instances of a , b.

can elaborate bit more on deal?


old answer:

have there madness merge 3 constructors , make seem `workerbee` , `employee` instantiated while not.

i'm not going question new a().b().c() notation though find quite messed up.

you want make use of instanceof operator following way.

var = function (x) {   if (!(this instanceof a)) return new a(x);    this.x = x; };  var b = function (y) {   if (!(this instanceof b)) return new b(y);    this.y = y; };  var c = function () {   if (!(this instanceof c)) return new c(); };  a.prototype.b = b; b.prototype.c = c; 

you can call new a() , a(), new b() , b() , new c() , c() interchangeably while achieving same result both of calls return instance of constructor.

new a() instanceof === true new a().b() instanceof b === true new a().b().c() instanceof c === true 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -