0

something is not totally make sense in JS constructors. when I doing something like this:

function extends(child, parent) {
    child.prototype = Object.create(parent.prototype);
    child.prototype.constructor = child;
};

the "constructor" is never called. meaning, the "child" function does called, because it the one we doing "new child()". but if I am doing something like this:

function extends(child, parent) {
    child.prototype = Object.create(parent.prototype);
    child.prototype.constructor = function() {
        alert("test");};
};

the "alert" is never fire because the real "child" function is called any way.

why is that? how can I override the constructor to be what I want ?

Nisim Joseph
  • 2,262
  • 22
  • 31
  • Just pass in the overwritten `child` to that `extends` function? – Bergi Mar 01 '16 at 19:43
  • When instantiating a `new child()`, the `child` constructor function will be called. The value of `….prototype.constructor` [doesn't matter at all](http://stackoverflow.com/q/4012998/1048572). – Bergi Mar 01 '16 at 19:44

1 Answers1

0

I understood the "issue". the constructor is never called in creation. it just for knowing what is the object type using the "prototype.constructor". you can't give it different function to run unless that function is the constructor you want to run.

Nisim Joseph
  • 2,262
  • 22
  • 31