2

I'm a JS newbie and I've been playing around the object creation and cloning with create () and assign () when I encountered the following error:

let Student = {
  first_name : '',
  last_name : '',
  major : '',
  IsNotSet : function () {
    return (this.first_name == '') 
        && (this.last_name == '') 
        && (this.major == '');
  },
  PrintDetails : function () {
    if (this.IsNotSet ()) {
      console.log ('Student is not set. Information cannot be retrieved.');
    } else {
      console.log ('Student Information:'
        + '\nFirst Name: ' + this.first_name
        + '\nLast Name: ' + this.last_name
        + '\nMajor: ' + this.major
      );
    }
  }
};

let StudentWithMinor = Object.assign ({}, Student,
  {
    minor : '',
    IsNotSet : function () {
      return (Student.IsNotSet.bind (this))() && (this.minor == '');
    },
    PrintDetails : function () {
      (Student.PrintDetails.bind (this))();
      if (!this.IsNotSet ()) {
        console.log ('Minor: ' + this.minor);
      }
    }
  }
);

let first_student = Object.create (Student);
first_student.first_name = 'Andrea';
first_student.last_name = 'Chipenko';
first_student.major = 'B.S.E Computer Engineering';
first_student.PrintDetails ();

let second_student = Object.assign ({}, Student);
second_student.first_name = 'Enzo';
second_student.last_name = 'D\'Napolitano';
second_student.major = 'B.S. Computer Science';
second_student.PrintDetails ();


let third_student = Object.create (StudentWithMinor);
third_student.first_name = 'Kumar';
third_student.last_name = 'Patel';
third_student.major = 'B.A. Business Administration';
third_student.minor = 'Criminal Justice';
third_student.PrintDetails ();

let fourth_student = Object.assign ({}, third_student);
// The following line is problematic
fourth_student.PrintDetails ();

I am not very sure as to why the last line would error out. Can any experts out there perhaps give me an insight as to what is happening internally?

Thanks you so much in advance!

R34
  • 59
  • 6
  • 2
    What's the error? – TimoStaudinger Jan 22 '18 at 21:22
  • 1
    @trincot they are not copied not because they are not "enumerable" but because they belong to prototype. `console.log(Object.keys(Object.getPrototypeOf(third_student)))`. "Object.assign does not copy methods" --- this is wrong, since `Object.assign` does not differentiate between types of the values. – zerkms Jan 22 '18 at 21:28

2 Answers2

2

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

Since third_student.PrintDetails is not its own property (it belongs to its prototype), it is not copied.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
  • Ahh I see. I thought 'Student' and 'first_student' would be the same as they are both typeof () == objects. Anyways thank you so much for this insightful answer :D – R34 Jan 22 '18 at 21:41
0

To clone an object, you also need to use the same prototype object - do not just assign the own properties of the cloned object to {}:

let fourth_student = Object.assign(Object.create(StudentWithMinor), third_student);

or

let fourth_student = Object.assign(Object.create(Object.getPrototypeOf(third_student)),
                                   third_student);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375