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"
:
String
or an Object
?String
or String.prototype
?String
and String.prototype
?String
object and String.prototype
the String prototype?Please bring clarity to this.
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.
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.
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.
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.
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:
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
.undefined
.0
, 3.1416
, 1000
, etc.. Also NaN
, and Infinity
.true
and false
."cat"
and "bar"
.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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With