GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • JavaScript Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Constructor in JavaScript - SyntaxDB - JavaScript Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Console Output
      • Commenting
      • Read from Text File
    • Variables
      • Variable Declaration
      • Global Variables
      • Arrays
      • Determine Type
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Functions
      • Function Declaration
      • Anonymous Functions
      • Closures
      • Function Assignment
    • Objects
      • Object Declaration
      • Methods
      • Create Object using Constructor
      • Constructor
Categories
Language
JavaScript
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 1.8.5

Categories

  • User Program Communication
    • Console Output
    • Commenting
    • Read from Text File
  • Variables
    • Variable Declaration
    • Global Variables
    • Arrays
    • Determine Type
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Functions
    • Function Declaration
    • Anonymous Functions
    • Closures
    • Function Assignment
  • Objects
    • Object Declaration
    • Methods
    • Create Object using Constructor
    • Constructor

Objects

Constructor in JavaScript

A constructor is a function used as a prototype for an object, and when called, create a new object instance from the prototype.


Syntax
function constructorName(passedParameters) {
    //set properties and run methods needed on object creation
    this.property1 = property1; //this keyword used to reference object
}

//adding a new property to a prototype
constructorName.prototype.newProperty = newPropertyValue;
var newObject = new constructorName(passedParams);

Notes

The 'new' keyword is used to create a new object from a function.


Example
function carConstructor(wheels, make){
    this.wheels = wheels;
    this.make = make;
}
carConstructor.prototype.mirrors = 3;
var newCar = new carConstructor(4, 'Mercedes');

See Also
Documentation

Working with objects - JavaScript | MDN

Add to Slack

< Create Object using Constructor   |   Console Output >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.