JavaScript is not an object-oriented language, but we construct it to support object-oriented, so that we can implement object-oriented features such as inheritance, rewriting, and so on. The specific code is as follows:
//create a class personfunctionPerson (age,name) { This. age=Age ; This. name=name; This. sayhello=function() {Console.log ("My lord, Hello, I call" + This. name+ ", this year" + This. age+ "Years old"); }}//Create class student, add attribute numberfunctionStudent (Age,name,stuno) { This. stuno=Stuno; Person.call ( This, age,name);//inherit the person class, taking care to differentiate between call and apply //rewrite SayHello This. sayhello=function() {Console.log ("Good student everyone, I call" + This. name+ ", this year" + This. age+ "years old, school number is:" + This. Stuno); }}varp=NewPerson (20, "John Doe");p. SayHello ();varstu=NewStudent (22, ' Harry ', ' 123456 '); Stu.sayhello ();
This small piece of code can elicit the difference between call and apply, the scope of this in JavaScript, and so on.
Javascript object-oriented-inheritance