0

what is different between Object.create(functionname.prototype) vs new Student() constructor call.

    function Student(name){
        this.name = name;
    }
     function UniversityStudent(id){
        this.id= id;
    }


    // 1st way
    UniversityStudent.prototype = Object.create(Student.prototype);
    var std = new UniversityStudent(123);
    // but I cannot access std.name why ?

    // 2nd way
    UniversityStudent.prototype = new Student("Lasith Malinga");
   var std1 = new UniversityStudent(123);
   // When I use std1.name now then it can

When I use 1st way then I cannot access Student`s object properties, but I use 2nd way it can, What is difference. I think both way are same... Is it wrong ?

Sachintha Udara
  • 635
  • 1
  • 7
  • 22
  • Well, have you inspected the objects you created in the console? What `name` do you think will they have? – Bergi May 18 '17 at 15:39
  • Your edit significantly altered the question, for this situation have a look at [this answer](https://stackoverflow.com/questions/10898786/correct-javascript-inheritance/). However, my point remains: where would `std` even get a `.name` from? – Bergi May 18 '17 at 16:15
  • use any browser console and try std.name :D – Sachintha Udara May 18 '17 at 17:26
  • I don't need a browser console to see that it's undefined. I'm asking what value you did expect it to have, so that I can clear up your confusion. – Bergi May 18 '17 at 17:42
  • Pls what are u asking... I did not understand ? – Sachintha Udara May 18 '17 at 17:48
  • You are asking why you cannot access `std.name`. I ask what you expected to get when you accessed it. – Bergi May 18 '17 at 17:52
  • I dont need any clarification coz I got nice url https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create – Sachintha Udara May 19 '17 at 05:41

0 Answers0