Function prototype: Difference between revisions

Content added Content deleted
(Omit from Déjà Vu)
(Improved examples (look less like a Java knock-off))
Line 313: Line 313:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
JavaScript does use function prototypes, it doesn't care the type of function, only that it is indeed a function.
JavaScript does use function prototypes, it doesn't care the type of function, only that it is indeed a function.
<lang JavaScript>
<lang JavaScript>var randomFunction=function(a){this.someNumber=a;}; //Doesn't matter when the function uses parameters, named arguments, no arguments, or if it doesn't return nothing at all.

randomFunction.prototype.getSomeNumber=function(){return this.someNumber;}
ents
randomFunction.prototype.setSomeNumber=function(a){return this.someNumber=a;}//editing prototype explicitly
function List() {}
(new randomFunction(7)).getSomeNumber();//7</lang>

List.prototype.push = function() {
[].push.apply(this, arguments);
return this.length;
};

List.prototype.pop = function() {
return [].pop.call(this);
};

var l = new List();
l.push(5);
l.length; // 1
l[0]; 5
l.pop(); // 5
l.length; // 0



// A prototype declaration for a function that utilizes varargs
function List() {
this.push.apply(this, arguments);
}

List.prototype.push = function() {
[].push.apply(this, arguments);
return this.length;
};

List.prototype.pop = function() {
return [].pop.call(this);
};

var l = new List(5, 10, 15);
l.length; // 3
l[0]; 5
l.pop(); // 15
l.length; // 2

</lang>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==