JSPatterns.com » Blog Archive » Constructors

Constructors

A simple way to create objects in JavaScript is using object literal notation. An other way, a bit more involved is to use a constructor function.

var adam = new Person("Adam");
adam.say(); // "I am Adam"

The invocation of the constructor looks a bit like a class in other languages, but the constructor is still just a function. (JavaScript doesnt have classes). So the constructor is the blueprint, the recipe for the object.

And here's how the constructor Person is defined, it adds all members to the this object.

var Person = function(name) {
 
  this.name = name;
  this.say = function() {
    return "I am " + this.name;
  };
 
};

When you define a constructor, something like the following happens behind the scenes. It's as if you defined an object using the literal notation and then returned it.

var Person = function(name) {
  // var this = {};
  this.name = name;
  this.say = function() {
    return I am  + this.name;
  };
  // return this;
};

Tags: ,

Sorry, comments disabled and hidden due to excessive spam. Working on restoring the existing comments...

Meanwhile you can find me on twitter - @stoyanstefanov


hacklink panel bypass shell wso shell evden eve nakliyat hacklink satış hacklink paykasa hacklink hacklink satış hacklink panel hacklink al hacklink satis hacklink hacklink evden eve nakliyat istanbul evden eve nakliyat nakliyat nakliye evden eve nakliyat istanbul evden eve nakliyat nakliyat nakliye depolama Google film izle porno porno izle porno seyret seks hikayeleri Google Evden Eve Nakliyat İstanbul Evden Eve Nakliyat Nakliyat İstanbul Nakliyat istanbul Evden Eve Nakliyat Nakliye Firmaları Şehirler Arası Evden Eve Nakliyat