Jump to content

Is thst the real goal:??


Maisara-WD
 Share

Recommended Posts

Hipeace upon uIs the prototype property used to define a new property to an object,, tell me if I undestood well...I'm sorry for this silly questionthanxbye

Link to comment
Share on other sites

You can use prototype to add or change a class. The array class, for example, has a method called push that adds another element onto the end of the array. If you wanted to push an element and then alert the length of the array, you could make your own method:

Array.prototype.push2 = function (val){  this.push(val);  alert(this.length);}

Then when you make an array object you can access that method like the built-in ones.var list = new Array;list.push("1");list.push2("2");A class prototype is basically a blueprint of how the class works, Javascript lets you change the prototypes for the built-in classes.

Link to comment
Share on other sites

The benefits to modifying a class of objects come when you need to declare multiple instances of that object. Then the new attribute or method already exists; it doesn't have to be added on to each instance. So if you modified the Array object according to JSG's example, *every* array in that script will have the push2 method available to it automatically.

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
 Share

×
×
  • Create New...