Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototypes in Javascript

Tags:

javascript

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.

like image 542
ajsie Avatar asked Nov 10 '10 07:11

ajsie


People also ask

What is prototype in JavaScript example?

In JavaScript, every function and object has a property named prototype by default. For example, function Person () { this.name = 'John', this. age = 23 } const person = new Person(); // checking the prototype value console.

What is prototype js used for?

Prototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with XMLHttpRequest. Prototype also provides library functions to support classes and class-based objects.

What is prototype in es6?

The prototype property allows you to add properties and methods to any object (Number, Boolean, String and Date, etc.). Note − Prototype is a global property which is available with almost all the objects. Use the following syntax to create a Boolean prototype.

Is JavaScript a prototype language?

JavaScript is a prototype-based language, meaning object properties and methods can be shared through generalized objects that have the ability to be cloned and extended.


2 Answers

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".
like image 131
Christian C. Salvadó Avatar answered Oct 14 '22 04:10

Christian C. Salvadó


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();
like image 22
Sebastian Schmidt Avatar answered Oct 14 '22 05:10

Sebastian Schmidt