First, let's assume that we need a say method to use one sentence, and a walk method to represent taking a step. Then let's implement the definition of the two methods in five ways.
1. Function say ()
{
Alert ("Hello World ");
}
Function walk ()
{
Alert ("walk ")
}
2.
VaR person = function (){};
Person. propotyoe, say = function ()
{
Alert ("Hello World ");
}
Person. Prototype. Walk = function ()
{
Alert ("walk ");
}
3. var person = {};
Person. Prototype = {
Say: function (){
Alert ("Hello World ")
}
Walk: function ()
{
Alert ("walk ");
}
}
4.
Function. Prototype. method = function (name, FN)
{
This. protype [name] = FN;
}
VaR person = function (){};
Person. Method ("say" function (){
Alert ("Hello World ")
})
Person. Method ("walk" function (){
Alert ("walk ")
})
5.
Function. Prototype. method = function (name, FN)
{
This. protype [name] = FN;
Return this;
}
VaR person = function (){};
Person. Method ("say" function (){
Alert ("hello ")
}).
Method ("walk" function (){
Alert ("walk ")
})