?
Solved

constructor definition in javascript

Posted on 2014-08-26
6
Medium Priority
?
262 Views
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.

Question-1

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

Question-2

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) ?

Question-3.

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

Question-4.

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

Please advise

Rwniceing
0
Comment
Question by:rwniceing
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 2000 total points
ID: 40285556
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
http://javascript.info/tutorial/constructor
0
 
LVL 12

Assisted Solution

by:trinitrotoluene
trinitrotoluene earned 2000 total points
ID: 40285560
The constructor property returns the constructor function for an object. This enables you to decide the type of the function object.
0
 
LVL 1

Author Comment

by:rwniceing
ID: 40285610
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

Rwniceing
0
 
LVL 1

Author Comment

by:rwniceing
ID: 40285987
The question-2 answer  is referred to  question-1 rely  and  tutorial constuctor javascript link at http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
and Question-3 and native code is referenced in , for example, for chrome browser V8 engine which is written in C++ at  https://code.google.com/p/v8/
0
 
LVL 1

Author Closing Comment

by:rwniceing
ID: 40292104
Thanks for your reply
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Loops Section Overview
Screencast - Getting to Know the Pipeline

839 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