prototype object javascript
Hi,
I have question about the prototype object. Mozilla says:
You can add new properties or methods to an existing class by adding them to the protoype associated with the constructor function for that class.
syntax is: fun.prototype.name = value
Consider this code
function myfunc( )
{
....
};
myfunc.prototype = { idx1 : function( a ) { ... }, idx2 : function( b ) {... },
idx3: function( c ) { ... } };
what does this mean?
It is assigning a hash to something!, according to syntax for prototype this does not make sense. Will prototype be a new property of myfunc if used like this?
Can someone please explain?
[738 byte] By [
venAdder] at [2007-11-20 4:53:30]

# 1 Re: prototype object javascript
Although they seem difficult at first, prototypes aren't that hard. Rather than explain it in my own words, take a look here ( http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:prototype). The part right above the example might best explain it for you.
# 2 Re: prototype object javascript
I already been there, that is where I got the definition from .
what i don't understand is:
myfunc.prototype = { idx1 : function( a ) { ... }, idx2 : function( b ) {... },
idx3: function( c ) { ... } };
see there is no '.' after prototype to define a property or a function. Following would have made sense,
myfunc.prototype.prop1 = { idx1 : function( a ) { ... }, idx2 : function( b ) {... },
idx3: function( c ) { ... } };
then I could just go
var obj = new myfunc( );
var sth = obj.prop1.idx1;
but with
myfunc.prototype = { idx1 : function( a ) { ... }, idx2 : function( b ) {... },
idx3: function( c ) { ... } };
how do i refer to some value at some index int aht half. I mean where is the identifier?
# 3 Re: prototype object javascript
see there is no '.' after prototype to define a property or a function. Following would have made sense,
It makes complete sense. In fact, that is why I reference the example shown from the other page.
There is no . after prototype nor should there be. It is basically just create a function with sub-functions.
function MyFunction() {
alert("Created.");
}
MyFunction.prototype = {
alert1: function(str) {
alert(str);
},
five: 5,
alert2: function() {
alert("Hi.");
}
};
var myObject = new MyFunction();
myObject.alert1("There.");
myObject.five;
myObject.alert2();
# 4 Re: prototype object javascript
aaaah, Okie. Thanx a lot friend. I am just new to javascript and when I saw that code, i was like what!!
# 5 Re: prototype object javascript
aaaah, Okie. Thanx a lot friend. I am just new to javascript and when I saw that code, i was like what!!
That's okay. Every one starts somewhere. Just out of curiosity, if you are a beginner, where do you plan to use prototypes? That is quite a step to start at.