-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfunction.jsdoc
More file actions
172 lines (123 loc) · 4.85 KB
/
Copy pathfunction.jsdoc
File metadata and controls
172 lines (123 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Function : Object
Functions are reusable pieces of code that can be executed with
different parameters.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3
----
Function([param1 : String, [param2 : String, [...]]], body : String) : Function
Same as %%#new_Function_String_String_dotdotdot_String|**new Function(param1, param2, ..., body)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.1.1
----
new Function([param1 : String, [param2 : String, [...]]], body : String) : Function
<p>Creates a new normal **Function** that has the supplied parameter names and body.
If any parameter name contains a **','**, it will be split on the **','** and
each component will be added as a parameter. Unless the **body** needs to
be modified at run time, **Function**s are typically created with the
**function** keyword.</p>
<p>See example below on how to construct generator (**function*()**) and async
functions (**async function()**).</p>
<example>
// The following result in equivalent behavior, but the first
// should be used unless the body changes at run time.
var f1 = function(x, y) { return x + y; };
var f2 = Function('x', 'y','return x + y;');
var f3 = Function('x, y', 'return x + y;');
var f4 = new Function('x', 'y', 'return x + y;');
// The following demonstrate creating generator and async functions
var GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor;
// The following are equivalent:
var g1 = function*(x, y) { yield x; yield y; }
var g2 = GeneratorFunction('x', 'y', 'yield x; yield y;');
var g3 = new GeneratorFunction('x, y', 'yield x; yield y;');
var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
// The following are equivalent:
var a1 = async function(url) { return (await fetch(url)).status; }
var a2 = AsyncFunction('url', 'return (await fetch(url)).status;');
var a3 = new AsyncFunction('url', 'return (await fetch(url)).status;');
var AsyncGeneratorFunction = Object.getPrototypeOf(async function*(){}).constructor;
// The following are equivalent:
var ag1 = async function*(x, y) { yield await x; yield await y; }
var ag2 = AsyncGeneratorFunction('x', 'y', 'yield await x; yield await y;');
var ag3 = new AsyncGeneratorFunction('x, y', 'yield await x; yield await y;');
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.2.1
----
prototype.apply([thisArg : Object, [parameters : Array]]) : Object
Call **this** with the **this** value inside the function bound to **thisArg**
and the parameters to the function from **parameters**. Returns the result
of the function call. See %%/Reflect.apply|Reflect.apply()%%.
<example>
var whatsThis = function() { console.log(this); }
whatsThis.apply('hello');
// Call a function that takes a variable number of args
var numbers = [3, 20, 1, 45];
console.log(Math.max.apply(undefined, numbers));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.3
----
prototype.call([thisArg : Object, [param1 : Object, [param2 : Object, [...]]]]) : Object
Call **this** with the **this** value inside the function bound to **thisArg**
and parameters **(param1, param2, ...)**. Returns the result of the function call.
<example>
var whatsThis = function() { console.log(this); }
whatsThis.call('hello');
// Call a function that takes a variable number of args
console.log(Math.max.call(undefined, 3, 20, 1, 45));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.4
----
prototype.bind(thisArg : Object, [param1 : Object, [param2 : Object, [...]]]) : Function
Returns a new function that, when called, will have **this** equal to **thisArg**,
the first parameter equal to **param1**, the second parameter equal to **param2**,
etc.
<example>
var Button = function(content) {
this.content = content;
};
Button.prototype.click = function() {
console.log(this.content + ' clicked');
}
var myButton = new Button('OK');
myButton.click();
var looseClick = myButton.click;
looseClick();
var boundClick = myButton.click.bind(myButton);
boundClick();
// Example showing binding some parameters
var sum = function(a, b) {
return a + b;
};
var add5 = sum.bind(null, 5);
console.log(add5(10));
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.5
----
instance.length : Number
The number of parameters specified when the function was defined.
<example>
var add = function(x, y) { return x + y; };
console.log(add.length);
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.1
----
instance.prototype : Object
The prototype for objects created by calling this function
with the **new** keyword.
<example>
var Button = function(content) {
this.content = content;
};
Button.prototype.click = function() {
console.log(this.content + ' clicked');
}
var myButton = new Button('OK');
myButton.click();
</example>
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.5.2