Jump to content

Understanding more about object.prototype.name


Stancobridge

Recommended Posts

Hello everyone, am new here and I believe that am going to get more help in this wonderful community...

 

Since am a beginner in web programming. I started with Javascript, on learning it I got stucked in String. Where we have Constructor, Prototype and Length.

 

 

I understood others but am still getting confused on Prototype.

 

I didn't understand really what they are trying to teach. I know it is used to add a new value to an object.

 

But the question is how? How can we use the added value? Where can we use it.

 

Please I need help on this..am waiting in anticipation. Thanks

Link to comment
Share on other sites

If you add a property or method to the prototype of any object then it becomes available on every instance of that object. e.g.:

 

String.prototype.showLength = function() {
  return this.toString() + " - length " + this.length;
}

var test = new String('some string');
console.log(test.showLength());
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...