JavaScript Javascript constructors inheritance javascript constructor inheritance,javascript constructor inheritance arguments,javascript constructor inheritance

Good Morning in my timezone.

I am learning JavaScript inheritance and i start reading the MDN pages. I understand that we have to use the prototype property from the constructor to build a inheritance chain, for example :

function Employee(){
 this.name = "Dave";
 this.dept = "";
}

function Manager(){
 this.projects = [];
}
Manager.prototype = new Employee;

If we do this :

var jane = new Manager();

jane.name -> It will retrive "Dave" because it will find on the Employee object.

What i can not understand is if you do this way:

 function Employee(name,dept){
 this.name = name || "Dave";
 this.dept = dept || "General";
}

function Manager(){
 this.base = Employee;
 this.base("Jack","CustpmDept");
 this.projects = [];
}

Now if i do the same :

var jane = new Manager();

jane.name -> It will retrive "Jack" because it will find on the Employee object.

In this last example i did not use the line Manager.prototype = new Employee; And it stil works , the Manager object have as their prototype object the Employee object. How is this possible ? Can you clarify me

Thanks in advance

Best regards

Answer:1

Check out the following code:

function Employee(name,dept){
    this.name = name || "Dave";
    this.dept = dept || "General";
    console.log(this instanceof Manager);
}

function Manager(){
     this.base = Employee;
     this.base("Jack","CustpmDept");
     this.projects = [];
}

var jane = new Manager();
console.log(jane.name);

In the example

console.log(this instanceof Manager);

returns true because when you call

this.base = Employee;

you are basically sending Manager's this to the Employee. this.name and this.dept are actually attached to the Manager.

Answer:2
function Manager(){
    this.base = Employee;
    this.base("Jack","CustpmDept");
    this.projects = [];
}

when you used the this.base("Jack","CustpmDept"), in invokes the Employee with this inside the Employee pointing to the new manager instance

Answer:3

The second way is like constructor stealing. this.base("Jack","CustpmDept"); will call the Employee constructor, since this key word will point to the object to be creates when we new the instance, so jane can get these properties.

Answer:4

In the latter, new Manager() returns 'Jack' because you call this.Employee('Jack', 'CustpmDept') which sets jane.name to 'Jack', because 'this' is 'Manager', or in this case, 'jane'.

It's a bit confusing.

Answer:5

I am working on a single page application called 'bookMarks' using HTML+CSS+jQuery as the front end and Django as my back end. I created a Django project and put my app.html+app.js+app.css+jquery.js ...

I am working on a single page application called 'bookMarks' using HTML+CSS+jQuery as the front end and Django as my back end. I created a Django project and put my app.html+app.js+app.css+jquery.js ...

  1. django return html string
  2. django return html file
  3. django return html response
  4. django return html template
  5. django return html
  6. django return html code
  7. django return html in json
  8. django return html ajax
  9. django return html tag
  10. django return html text
  11. django return static html
  12. django return render html
  13. django admin return html
  14. django httpresponse return html
  15. django view return html file
  16. django return raw html
  17. django filter return html
  18. django view return html string
  19. django rest return html
  20. django return safe html

I have a button and an image and want them to change color onmouseover. The button changes color fine: <script> function secondColor(x) { x.style.color="#000000"; } function firstColor(x) { x....

I have a button and an image and want them to change color onmouseover. The button changes color fine: <script> function secondColor(x) { x.style.color="#000000"; } function firstColor(x) { x....

  1. javascript make image clickable
  2. javascript make image appear on click
  3. javascript make image visible
  4. javascript make image transparent
  5. javascript make image bigger on hover
  6. javascript make image black and white
  7. javascript make image move across screen
  8. javascript make image background transparent
  9. javascript make image smaller
  10. javascript make image fit screen
  11. javascript make image bigger on click
  12. javascript make image invisible
  13. javascript make image grayscale
  14. javascript make image a button
  15. javascript make image
  16. javascript make image bounce around screen
  17. javascript make image disappear
  18. javascript make image full screen
  19. javascript make image spin
  20. javascript make image draggable

Using the bootstrap-tour plug-in (http://bootstraptour.com/), I want to disable or hide the default 'End Tour' button on each step except for the very last step. I tried modifying the step template ...

Using the bootstrap-tour plug-in (http://bootstraptour.com/), I want to disable or hide the default 'End Tour' button on each step except for the very last step. I tried modifying the step template ...

My original (working) code looks like: jQuery().ready(function ($) { $('[id="errorMessages"]').ajaxStart(function () { $(this).html(""); }); $('[id="errorMessages"]').ajaxError(...

My original (working) code looks like: jQuery().ready(function ($) { $('[id="errorMessages"]').ajaxStart(function () { $(this).html(""); }); $('[id="errorMessages"]').ajaxError(...