Function Constructors, ‘new’, and the History of Javascript

Something Like that:
Function Constructors, 'new', and the History

What is Function Constructors in javascript
A Normal function that is used to construct objects.
(The ‘this’ variable points a new empty object, and that object is returned from the function automatically.)

JS

function Person(){
    document.write(this + '<br>');
    this.firstname = 'John';
    this.lastname = 'Doe';
    document.write('This function is invoked.' + '<br>');
}

var john = new Person();
document.write(john + '<br>');

var jane = new Person();
document.write(jane + '<br>');

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”VzXOPy” default_tab=”result” user=”pradeepanvi”]See the Pen Function Constructors in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

[object Object]
This function is invoked.
[object Object]
[object Object]
This function is invoked.
[object Object]

Leave a Reply

Your email address will not be published. Required fields are marked *