logo
down
shadow

Javascript Inheritance with Parent Constructor Parameters


Javascript Inheritance with Parent Constructor Parameters

By : Josh
Date : November 19 2020, 03:01 PM
I think the issue was by ths following , Standard ES5 and below
For the specific case of your example, headmax's answer does what you need. However, what you're trying to do in a general sense (that is, classical inheritance) may be possible with C# and other languages with classical inheritance models, but it's not possible the same way with the JavaScript Prototype model. You can probably get closest to that using composition, like so:
code :
var Person = function(initName){
    var name = initName;
}

var Employee = function(initName, initCompany){
    var innerPerson = new Person(initName);

    var company = initCompany;
}

var employee1 = new Employee("Bob", "Intel");
var Person = function(initName){
    var name = initName;
}

var Employee = function(initName, initCompany){
    Person.call(this, initName);
    var innerPerson = new Person(initName);

    var company = initCompany;
}

var employee1 = new Employee("Bob", "Intel");


Share : facebook icon twitter icon
why do we need to call the parent constructor in JavaScript Inheritance

why do we need to call the parent constructor in JavaScript Inheritance


By : endofdays
Date : March 29 2020, 07:55 AM
this one helps. Running the example you provided only calls the Person() constructor once during initial execution of the script when the line "Student.prototype = new Person();" is executed.
If we modify your script to create a second student and separate the setup from the instantiation: bit: http://jsfiddle.net/anacW/
code :
function Person(gender) {
    this.gender = gender;
    document.write('Person instantiated</br>');
}

Person.prototype.walk = function(){
    document.write("is walking</br>");
};

Person.prototype.sayHello = function(){
    document.write("Hello</br>");
};

Person.prototype.sayGender = function(){
    document.write(this.gender + "</br>");
};



function Student() {
    //Person.call(this);
    document.write('Student instantiated</br>');        
}
Student.prototype = new Person();

Student.prototype.constructor = Student;

Student.prototype.sayHello = function(){
    document.write("Student says Hello</br>");
}
Student.prototype.sayGoodBye = function(){
    document.write("Student says goodbye</br>");
}

document.write("*** Building student1 *** </br>");
var student1 = new Student();
student1.sayHello();
student1.walk();
student1.sayGoodBye();

document.write("*** Building student2 ***</br>");
var student2 = new Student();
student2.sayHello();
student2.walk();
student2.sayGoodBye();

document.write("*** InstanceOf Tests ***</br>");
document.write("student1 is Person?: " + (student1 instanceof Person));
document.write("</br>");
document.write("student1 is Student?: " + (student1 instanceof Student));
document.write("</br>");
document.write("student2 is Person?: " + (student2 instanceof Person));
document.write("</br>");
document.write("student2 is Student?: " + (student2 instanceof Student));
Person instantiated
*** Building student1 *** 
Student instantiated
Student says Hello
is walking
Student says goodbye
*** Building student2 ***
Student instantiated
Student says Hello
is walking
Student says goodbye
*** InstanceOf Tests ***
student1 is Person?: true
student1 is Student?: true
student2 is Person?: true
student2 is Student?: true
Prototypal inheritance in JavaScript: I don't usually need calling the constructor of Parent object assigned to Child.pr

Prototypal inheritance in JavaScript: I don't usually need calling the constructor of Parent object assigned to Child.pr


By : Bob Broadway
Date : March 29 2020, 07:55 AM
it helps some times One thing you can do is assign the instance of the parent to it's prototype property in the constructor. This means that you do not have to create an extraneous instance of the parent and therefore alleviates the issue you mentioned where you could end up with an extra GUI component being defined. However, it does mean that you must have at least one instance of the parent before instantiating an instance of the child and therefore this alone limits its usefulness to very specific circumstances.
Here's an example: http://jsfiddle.net/xwwWZ/
code :
var Child = function() {

    this.throwATantrum = function() {

        alert("Waaaaaaaaaah");

    }
};


var Parent = function() {

    // Set the prototype here to avoid having to create an extra instance elsewhere.
    Parent.prototype = this;

    this.sayHello = function() {

        alert("Hello!");

    }
};


// We must, however, instantiate an instance of the parent before we can
// instantiate an instance of a child.
var p = new Parent();


Child.prototype = Parent.prototype;

var c = new Child();

c.throwATantrum();
c.sayHello();
Javascript Inheritance calling parent's non default constructor

Javascript Inheritance calling parent's non default constructor


By : Eric Montijo
Date : March 29 2020, 07:55 AM
will be helpful for those in need Here's a minimal example which achieves what you want (calling a parent constructor from within the constructor of a derived class):
code :
var Shape = function(sides) {
  this.sides = sides;
};

var Square = function(size) {
  /* Just call the parent constructor function with `this` as context. */
  Shape.call(this, 4);
  this.size = size;
};

/* Set up the prototype chain. Use a shim for `Object.create` if you want. */
Square.prototype = Object.create(Shape.prototype);
Function.prototype.inherits=function(parent)
{
    var ctor=this;
    var p=Object.create(parent);
    ctor.super=function()
    {
        parent.apply(p,arguments);
    }
    ctor.prototype=p;
}


function A(x)
{
    this.x=x;
}

function B(y)
{
    B.super(y*2);
    this.y=y;
}

B.inherits(A);

var b1 = new B(1);
var b2 = new B(2);
alert(b1.x); // displays "4" instead of "2"!
Javascript call Parent constructor in the Child (prototypical inheritance) - How it works?

Javascript call Parent constructor in the Child (prototypical inheritance) - How it works?


By : DSD
Date : March 29 2020, 07:55 AM
like below fixes the issue First of all, stop doing Child.prototype = new Parent(); for inheritance, unless your browser doesn't support any other alternative. That's a very bad style and can have undesired side effects, since it actually runs the constructor logic.
You can use Object.create in every modern browser now.
For JavaScript prototypal inheritance, why can't a parent object be created and saved in a child's constructor?

For JavaScript prototypal inheritance, why can't a parent object be created and saved in a child's constructor?


By : xiaoshier
Date : March 29 2020, 07:55 AM
around this issue Your prototype methods and inheritance should be defined outside of your functions...
Related Posts Related Posts :
  • Bootstrap DatePicker format mm/yyyy set max month
  • Firebase cloud firestore + auth: write only for signed in users
  • Getting jQuery.data functionality without jQuery
  • How to get incrementing serial numbers for new and removed items for jQuery sortable?
  • Highlighting a word or sentence in iframe, using javascript/Jquery
  • Calculate what percentage of a specific element has been scrolled into view
  • Knockout autocomplete with jquery doesn't allow to select custom value
  • react native - react-native-maps performance slow on iOS
  • ajax fallback when no internet connection
  • Show Textbox based on RadioButton selection or value when Page Loads
  • JS maximum call stack exceeded
  • Resetting object key values
  • How can i disable the toggle, preventing user to tap/click it?
  • How to include javascript file into LOV popups on apex oracle?
  • Javascript Angular 4 eventEmitter with ngClass
  • Webpack - module not found even though module exists
  • How to display following values using vue js?
  • Regexp: Allow only use of a few words and only once per word
  • CCapture.js webm video blacked out
  • Using a HTML hyperlink to call a JS function on the parent element
  • Return undefined from existing property in javascript model
  • What is the Difference Between These two jQuery Code Snippets?
  • How to get Network Speed in WebRTC
  • How to get text from selected value in a dropdownlist which is js based
  • window is not defined angular universal third library
  • Angularjs ng-repeat stylization depending on previous value
  • Trying to implement Fittext.js
  • Calculate number of match in array Lodash
  • Jquery Smooth Scroll Using Offset.top
  • How to extract data to React state from CSV file using Papa Parse?
  • How to add unique links to google maps markers
  • How to use if condition in a tool bar in java script
  • Ajax filter in django not showing in HTML
  • data collection with Javascript
  • Rotate image on lightbox2 load
  • Prevent body from scrolling when a Pop-Up is open
  • How to copy files that do not need to be compiled in Gulp?
  • Array not assigned to variable? How does this work and what exactly is it doing?
  • Sorting associative array of objects in javascript
  • Changing Icon in Sap.m.tree having CustomTreeItem
  • Merge two array of objects based on a key
  • javascript in css not working
  • Passing only clicked element to onClick function - reactjs
  • React boilerplate doesn't load js files in the index.html
  • is Child service inside child component visible in the Parent component?
  • Check if data attribute value equals a string
  • How to get value of child tag of a button tag
  • How to access subjects of selected mails in Apple Mail using JavaScript?
  • How to get all dynamically set inline-style CSS in jQuery?
  • Error: Module "html" does not provide a view engine (Express)
  • Random Image in <Div> from array
  • Slider with touch function
  • ReactJS Component Architecture Problems / Nested Components or Single Component Manager
  • Javascript: Caching within Closure doesn't work
  • HTM5 Canvas Drawing App: How Do I Select The Color?
  • Assigning Events using HTML DOM
  • html5 getUserMedia() portrait mode
  • How to avoid 'headers already sent' within Promise chain?
  • Get a result from a react native app integrated into an existing android app
  • Why does the value of input field return undefined
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org