Constructor Functions - Node.js Object

Node.js examples for Object:Constructor

Description

Constructor Functions

Demo Code


// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

function Person() {
    this.name = 'Isak';
    this.greet = function() {
        console.log('Hello, I am ' + this.name);
    }/*from  w  w w . j  a  va  2s.  c o  m*/

}

Person.prototype.greetGeneral = function() {
    console.log('Hello');
};

// Contructor Function
var person = new Person();
person.name = 'Pelle';
//console.log(person.name);
person.greet();

var anotherPerson = new Person();
anotherPerson.greetGeneral();

console.log(person instanceof Person);



function Person(name, age) {
    this.name = name;
    this.age = age;
}

var isak = new Person('Isak', 34);
console.log(isak);

Related Tutorials