March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
36
Task/Function-prototype/JavaScript/function-prototype-1.js
Normal file
36
Task/Function-prototype/JavaScript/function-prototype-1.js
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// A prototype declaration for a function that does not require arguments
|
||||
function List() {}
|
||||
|
||||
List.prototype.push = function() {
|
||||
return [].push.apply(this, arguments);
|
||||
};
|
||||
|
||||
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() {
|
||||
return [].push.apply(this, arguments);
|
||||
};
|
||||
|
||||
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
|
||||
37
Task/Function-prototype/JavaScript/function-prototype-2.js
Normal file
37
Task/Function-prototype/JavaScript/function-prototype-2.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// A prototype declaration for a function that does not require arguments
|
||||
class List {
|
||||
push() {
|
||||
return [].push.apply(this, arguments);
|
||||
}
|
||||
pop() {
|
||||
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
|
||||
class List {
|
||||
constructor(...args) {
|
||||
this.push(...args);
|
||||
}
|
||||
push() {
|
||||
return [].push.apply(this, arguments);
|
||||
}
|
||||
pop() {
|
||||
return [].pop.call(this);
|
||||
}
|
||||
}
|
||||
|
||||
var l = new List(5, 10, 15);
|
||||
l.length; // 3
|
||||
l[0]; 5
|
||||
l.pop(); // 15
|
||||
l.length; // 2
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
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;}
|
||||
randomFunction.prototype.setSomeNumber=function(a){return this.someNumber=a;}//editing prototype explicitly
|
||||
(new randomFunction(7)).getSomeNumber();//7
|
||||
Loading…
Add table
Add a link
Reference in a new issue