‘new’ and functions in javascript

Something Like that:
'new' and functions

JS

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

Person.prototype.getFullName = function(){
    return this.firstname + ' ' + this.lastname;
}

var john = Person('Johan','de');
document.write(john + '<br>');

var jane = Person('Jane','de');
document.write(jane + '<br>');

Person.prototype.getFormalFullName = function(){
    return this.lastname + ', ' + this.firstname;
}

document.write(john.getFormalFullName() + '<br>');

[codepen_embed height=”265″ theme_id=”0″ slug_hash=”eEMabg” default_tab=”result” user=”pradeepanvi”]See the Pen ‘new’ and functions in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now you can see

[object Window]
This function is invoked.
undefined
[object Window]
This function is invoked.
undefined

Leave a Reply

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