first on the code :
Example of the Apply () method
/*Define a human*/ functionPerson (name,age) { This. name=name; This. age=Age ; } /*define a student class*/ functionStudent (name,age,grade) {person.apply ( This, arguments);//arguments namely ["Zhangsan", 21, "first Grade"] This. grade=grade; } //create an instance of a student classvarStudent=NewStudent ("Zhangsan", 21, "first Grade")); //TestConsole.log ("Name:" +student.name+ "\ n" + "Age:" +student.age+ "\ n" + "Grade:" +Student.grade); /*result output: Name:zhangsan age:21 Grade: First grade*/
Call () Method Example-1:
/*Define a human*/ functionPerson (name,age) { This. name=name; This. age=Age ; } /*define a student class*/ functionStudent (name,age,grade) {Person.call ( This, Name,age); This. grade=grade; } //create an instance of a student classvarStudent=NewStudent ("Zhangsan", 21, "first Grade")); //TestConsole.log ("Name:" +student.name+ "\ n" + "Age:" +student.age+ "\ n" + "Grade:" +Student.grade); /*result output: Name:zhangsan age:21 Grade: First grade*/
Call () Method Example-2:
/*Define a human*/ functionPerson (name,age) { This. name=name; This. age=Age ; } /*define a student class*/ functionStudent (name,age,grade) {Person.call ( This, name); This. grade=grade; } //create an instance of a student classvarStudent=NewStudent ("Zhangsan", 21, "first Grade")); //TestConsole.log ("Name:" +student.name+ "\ n" + "Age:" +student.age+ "\ n" + "Grade:" +Student.grade); /*result output: Name:zhangsan age:undefined Grade: First grade*/
The use of the JavaScript method call () and apply ()