5

Even though I have read a lot about it I can't get my head around the prototype concept.

Why is there a String and String.prototype?

If I have "cat":

  • Is that a String or an Object?
  • Does it inherits all properties/methods from String or String.prototype?
  • Why is there a String and String.prototype?
  • Should I call String the String object and String.prototype the String prototype?

Please bring clarity to this.

2 Answers 2

10

I'm answering this because there is a lot of misinformation in the subject:

Is that a String or an Object?

No "cat" is a primitive String value:

typeof "cat"; // "string", a String value
"cat" instanceof String; // false


typeof new String("cat"); // "object", a String object
new String("cat") instanceof String; // true

I will talk later on about types and primitive values.

Does it inherits all properties/methods from String or String.prototype?

Well, when you use the property accessor operator (the dot or the bracket notation), the primitive value is implicitly converted to object, internally, therefore all the methods on String.prototype are available, for example:

When you access:

"cat".chatAt(0);

Behind the scenes "cat" is converted to object:

Object("cat").chatAt(0);

That's why you have access to all the inherited properties on values.

Why is there a String and String.prototype?

String is a constructor function, allows you to create String objects or do type conversion:

var stringObj = new String("foo"); // String object

// Type conversion
var myObj = { toString: function () { return "foo!"; } };

alert(String(myObj)); // "foo!"

The String.prototype object, is the object where String object instances inherit from.

I know it's confusing, we have String values and String objects, but most of the time you actually work only with string values, don't worry for now about String objects.

Should I call String the String object and String.prototype the String prototype?

You should call String "The String constructor".

"String prototype" is ok.

You should know that "Everything is NOT an object".

Let's talk about types, there are five language types specified:

  • String
  • Number
  • Boolean
  • Null
  • Undefined

A primitive value is " a datum that is represented directly at the lowest level of the language implementation", the simplest piece of information you can have.

The values of the previously described types can be:

  • Null: The value null.
  • Undefined: The value undefined.
  • Number: All numbers, such as 0, 3.1416, 1000, etc.. Also NaN, and Infinity.
  • Boolean: The values true and false.
  • String: Every string, such as "cat" and "bar".
Sign up to request clarification or add additional context in comments.

2 Comments

I agree with you with the "string" and new String() difference, i admit that i didn't know that. But i don't agree with the part of datum and objects. Maybe strings, booleans and numbers are not objects but they are represented as objects in JS and if you do something like String instanceof Object the result is true.
@mck89, I meant that a primitive value is the simplest piece of data that you can have, yes, you can "treat them as objects", (e.g. accessing inherited properties, "foo".chatAt or 5..toFixed), but they still are just that, values, not objects. About String instanceof Object is obvious because String is a Function object, that inherits from Function.prototype, which inherits from Object.prototype, therefore, String instanceof Function will give you also true.
2

Strings are Objects in JavaScript. There is no real datatype called string like in PHP, C, or other languages.

String.prototype ist a construct to add functionality to all your String-Objects. If you want all Strings to offer for example a conversion function you do the following:

String.prototype.convert = function () {
   // Put your code in here
}

// Set up y new String
var example = "cat";

// Now you can call your function
example.convert();

1 Comment

That's not right, JavaScript has a String type, "cat" is a primitive value. JavaScript can have also String objects but the concept is different, they are simply wrapped primitives, e.g. alert(typeof "foo"); // "string", alert(typeof Object("cat")) // "object". Check my answer.

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.