constructor definition in javascript

Posted on 2014-08-26
Last Modified: 2014-08-29
Dear Experts,

I get confused with the terminology, constructor, in javascript , it always show in javascript
document or internet article.


For example,
function test() {}
var b= new test()
where test() is function constructor always described in those article   What is constructing ?


var mesg="hello";
console.log(mesg); //hello
var mess = new String("hello") ;
console.log(mesg) //String{0:"h",1:"e",2:"l",3:"l",4:"o",Length:5}
console.log(mesg.constructor);  //function String(){ [ native code ] }

What is mesg constructing from mesg.constructor ? What there is [native code] meaning ?
What is native meaning in javascript( i.e. bulit-in code or function) ?


Could we show all String function code inside instead of [native code], OR it is not allowed on browser ?


Is it exactly same meaning of constructor in javascript as in C++ such using "::"

Please advise

Question by:rwniceing
    LVL 12

    Accepted Solution

    constructor can be viewed as a prototype from which an object is created. You can retrieve the constructor for a function object and it can be used if you need to relate a function object to its creation.

    In a way it is similar to the constructor concept that is seen in other OO languages like C++ and Java.

    Take a look at
    LVL 12

    Assisted Solution

    The constructor property returns the constructor function for an object. This enables you to decide the type of the function object.
    LVL 1

    Author Comment

    You're answering  Q1 and  Q4
    In your reply to question 1,
    string.prototype = { constructor: function(){ [native code]} }
    So  constructor  is just   object string  protoptype's property which is named  with constructor.
    Accessing Sting.constructor will show function(){[native code]} such as in quesiton-2
    So constructor is just built-in object prototype property, nothing is special, Right ?

    To question-4, I think so,  other epxerts agree it ?

    Could you also answer Q2 and Q3, thanks for your reply

    LVL 1

    Author Comment

    The question-2 answer  is referred to  question-1 rely  and  tutorial constuctor javascript link at
    and Question-3 and native code is referenced in , for example, for chrome browser V8 engine which is written in C++ at
    LVL 1

    Author Closing Comment

    Thanks for your reply

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now