Js interview questions-inheritance of js, js questions-js
Js is a flexible language. There are often multiple ways to implement a function. ECMAScript does not have a clear Inheritance Mechanism, but is achieved through imitation. According to the characteristics of js, js implementation inheritance has the following common methods:
1. Use object impersonating to implement inheritance (this implementation method can implement multiple inheritance)
Implementation principle: Let the constructor of the parent class become the sub-class method, then call the sub-class method, assign values to all attributes and methods through the this keyword
Js Code
- Function Parent (firstname)
- {
- This. fname = firstname;
- This. age = 40;
- This. sayAge = function ()
- {
- Console. log (this. age );
- }
- }
- Function Child (firstname)
- {
- This. parent = Parent;
- This. parent (firstname );
- Delete this. parent;
- This. saySomeThing = function ()
- {
- Console. log (this. fname );
- This. sayAge ();
- }
- }
- Var mychild = new Child ("Li ");
- Mychild. saySomeThing ();
2. Use the call method to change the context of the function to implement inheritance (this method cannot inherit the prototype chain. If you want to inherit the prototype chain, use the 5 hybrid mode)
Implementation principle: change the context of the function. this directs it to the specific object of the input function.
Js Code
- Function Parent (firstname)
- {
- This. fname = firstname;
- This. age = 40;
- This. sayAge = function ()
- {
- Console. log (this. age );
- }
- }
- Function Child (firstname)
- {
- This. saySomeThing = function ()
- {
- Console. log (this. fname );
- This. sayAge ();
- }
- This. getName = function ()
- {
- Return firstname;
- }
- }
- Var child = new Child ("Zhang ");
- Parent. call (child, child. getName ());
- Child. saySomeThing ();
3. Use the Apply method to change the context of the function to implement inheritance (this method cannot inherit the prototype chain. If you want to inherit the prototype chain, use the 5 hybrid mode)
Implementation principle: change the context of the function. this directs it to the specific object of the input function.
Js Code
- Function Parent (firstname)
- {
- This. fname = firstname;
- This. age = 40;
- This. sayAge = function ()
- {
- Console. log (this. age );
- }
- }
- Function Child (firstname)
- {
- This. saySomeThing = function ()
- {
- Console. log (this. fname );
- This. sayAge ();
- }
- This. getName = function ()
- {
- Return firstname;
- }
- }
- Var child = new Child ("Zhang ");
- Parent. apply (child, [child. getName ()]);
- Child. saySomeThing ();
4. Implement inheritance using prototype chain
Implementation principle: enables the subclass prototype object to point to the parent class instance for inheritance, that is, the prototype of the override class. The disadvantage is that multiple inheritance cannot be implemented directly.
Js Code
- Function Parent ()
- {
- This. sayAge = function ()
- {
- Console. log (this. age );
- }
- }
- Function Child (firstname)
- {
- This. fname = firstname;
- This. age = 40;
- This. saySomeThing = function ()
- {
- Console. log (this. fname );
- This. sayAge ();
- }
- }
- Child. prototype = new Parent ();
- Var child = new Child ("Zhang ");
- Child. saySomeThing ();
5. Implement inheritance in hybrid mode
Js Code
- Function Parent ()
- {
- This. sayAge = function ()
- {
- Console. log (this. age );
- }
- }
- Parent. prototype. sayParent = function ()
- {
- Alert ("this is parentmethod !!! ");
- }
- Function Child (firstname)
- {
- Parent. call (this );
- This. fname = firstname;
- This. age = 40;
- This. saySomeThing = function ()
- {
- Console. log (this. fname );
- This. sayAge ();
- }
- }
- Child. prototype = new Parent ();
- Var child = new Child ("Zhang ");
- Child. saySomeThing ();
- Child. sayParent ();
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.