0

I've been reading tutorials for a few weeks now and I've just figured out that when I use the prototype property on a constructor function, the key/value pairs on that prototype property are copied to the proto property of the newly instantiated object.

function F(){};
F.prototype.k = "v";
console.dir(F.k)//logs "undefined"
console.dir(F.prototype.k)//logs "v"

var o = new F;
console.dir(o.k)//logs "v"

So, key "k" is on the prototype property in the constructor and then transfers this to the proto property of newly instantiated object "o" which is why it can be accessed as if it were just a normal key/value pair on the object. Ok, that makes sense to me...but I thought about it and I've seen people use the new keyword for built-in things like String (although this is usually not done)

var s = new String;

the above code is an example of the way a new string item could be created the same way objects can be instantiated based on constructor functions. This made me wonder "is String just a constructor function????!" So I tested this out:

console.dir(String.prototype)

And I got a whole list of exactly the same properties that are attached to s. So, is "String" just a constructor function? The same behavior seems to be true of these items:

console.dir(String.prototype);
console.dir(Number.prototype);
console.dir(Boolean.prototype);
console.dir(Array.prototype);
console.dir(Object.prototype);
console.dir(Function.prototype);
console.dir(Date.prototype);

They all appear to behave exactly the same as constructor functions. They are even all capitalized rather than camelcase. Are they just constructor functions with a few built-in bells and whistles added to them?

17
  • Yes, they are constructors, but some primitive literals don't exatly correspond to an instance of the type constructor, in the case of strings, or numbers for example, that's why you shouldn't use new String, or new Number. Commented Jul 2, 2014 at 20:36
  • The primitives have their own constructors but it's considered bad practice to modify them. Commented Jul 2, 2014 at 20:37
  • Yeah I noticed that also...If I instantiate an object from something like String: var s = new String; console.dir(typeof s); it returns "object" not string. And yet if I type console.dir(typeof "hello"); it returns string. I don't understand why this happens but I'm guessing something flashy behind the scenes is going on. "typeof" doesn't produce the constructor function type, it seems to be reflecting something else. Commented Jul 2, 2014 at 20:39
  • It's not clear what has surprised you: the fact that prototype is "copied" into instance? Commented Jul 2, 2014 at 20:40
  • 1
    @dandavis: well, who does define those requirements for the function to be treated as a constructor? Is it somewhere in ECMAScript? Commented Jul 2, 2014 at 20:52

1 Answer 1

3

There are 6 data types in JavaScript:

  • Boolean
  • Number
  • String
  • Null
  • Undefined

  • Object

The first five are so called primitive types. Three of the primitive types, Boolean, Number and String, also have an equivalent Object implementation.

Whenever you use (Boolean|Number|String) literals, you are creating a value of that corresponding type.

You can also create Boolean, Number and String objects by calling the corresponding constructor function with the new operator:

var s = 'foo'; // type string
var s2 = new String('foo'); // type object

Now, the reason why you can access properties on both of them is that JavaScript auto-boxes primitive values. I.e. when you are trying to access a property on a primitive value, it will temporarily convert the value to an object and access the property.

So

var v = "foo";
alert(v.length);

is essentially

var v = "foo";
alert((new String(v)).length);

If the String, Number or Boolean function is called without the new keyword, they will return a primitive value instead, which makes them act as conversion functions.


And to round this up: Every function has a prototype property, every function can potentially act as a constructor. Whether it really does depends on the implementation of the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.