What is parent method in javascript?

In case of multiple inheritance level, this function can be used as a super() method in other languages. Here is a demo fiddle, with some tests, you can use it like this, inside your method use : call_base(this, 'method_name', arguments);

It make use of quite recent ES functions, an compatibility with older browsers is not guarantee. Tested in IE11, FF29, CH35.

/**
 * Call super method of the given object and method.
 * This function create a temporary variable called "_call_base_reference",
 * to inspect whole inheritance linage. It will be deleted at the end of inspection.
 *
 * Usage : Inside your method use call_base(this, 'method_name', arguments);
 *
 * @param {object} object The owner object of the method and inheritance linage
 * @param {string} method The name of the super method to find.
 * @param {array} args The calls arguments, basically use the "arguments" special variable.
 * @returns {*} The data returned from the super method.
 */
function call_base(object, method, args) {
    // We get base object, first time it will be passed object,
    // but in case of multiple inheritance, it will be instance of parent objects.
    var base = object.hasOwnProperty('_call_base_reference') ? object._call_base_reference : object,
    // We get matching method, from current object,
    // this is a reference to define super method.
            object_current_method = base[method],
    // Temp object wo receive method definition.
            descriptor = null,
    // We define super function after founding current position.
            is_super = false,
    // Contain output data.
            output = null;
    while (base !== undefined) {
        // Get method info
        descriptor = Object.getOwnPropertyDescriptor(base, method);
        if (descriptor !== undefined) {
            // We search for current object method to define inherited part of chain.
            if (descriptor.value === object_current_method) {
                // Further loops will be considered as inherited function.
                is_super = true;
            }
            // We already have found current object method.
            else if (is_super === true) {
                // We need to pass original object to apply() as first argument,
                // this allow to keep original instance definition along all method
                // inheritance. But we also need to save reference to "base" who
                // contain parent class, it will be used into this function startup
                // to begin at the right chain position.
                object._call_base_reference = base;
                // Apply super method.
                output = descriptor.value.apply(object, args);
                // Property have been used into super function if another
                // call_base() is launched. Reference is not useful anymore.
                delete object._call_base_reference;
                // Job is done.
                return output;
            }
        }
        // Iterate to the next parent inherited.
        base = Object.getPrototypeOf(base);
    }
}


In order to call the parent method when both parent and child have the same method name and signature.

You can use the below syntax −

console.log(yourParentClassName.prototype.yourMethodName.call(yourChildObjectName));

Example

class Super {
   constructor(value) {
      this.value = value;
   }
   display() {
      return `The Parent class value is= ${this.value}`;
   }
}
class Child extends Super {
   constructor(value1, value2) {
      super(value1);
      this.value2 = value2;
   }
   display() {
      return `${super.display()}, The Child Class value2
      is=${this.value2}`;
   }
}
var childObject = new Child(10, 20);
console.log("Calling the parent method display()=")
console.log(Super.prototype.display.call(childObject));
console.log("Calling the child method display()=");
console.log(childObject.display());

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo192.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo192.js
Calling the parent method display()= The Parent class value is= 10
Calling the child method display()= The Parent class value is= 10, The Child Class value2 is=20

What is parent method in javascript?

Updated on 14-Sep-2020 08:31:46

  • Related Questions & Answers
  • Can JavaScript parent and child classes have a method with the same name?
  • How do I invoke a Java method when given the method name as a string?
  • Can we define a method name same as class name in Java?
  • Can a method have the same name as the class?
  • Apply style to the parent if it has a child with CSS and HTML
  • How to invoke a JavaScript Function as a method?
  • When can we use the pack() method in Java?
  • When can we use the getClass() method in Java?
  • How to get the child element of a parent using JavaScript?
  • Can we define multiple methods in a class with the same name in Java?
  • How to name a data frame column with a vector value that has the same name in R?
  • How can create a table having the name like a^b along with same column name? name?
  • If a method in parent class “throws Exception”, can we remove it in overridden method in java?
  • When can we use StackWalker.getCallerClass() method in Java 9?
  • How to invoke a function as a function and method?

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

What is a parent element in code?

A parent is an element that is directly above and connected to an element in the document tree. In the diagram below, the <div> is a parent to the <ul>. A child is an element that is directly below and connected to an element in the document tree.

What is parent in jQuery?

The parent() is an inbuilt method in jQuery which is used to find the parent element related to the selected element. This parent() method in jQuery traverse a single level up the selected element and return that element. Syntax: $(selector).parent() Here selector is the selected elements whose parent need to find.

What is parent div?

A parent element is usually the containing element, with the elements inside being its child or children. In Yulias example above, the 'div' would be the parent and the 'img' being the child.