/
apply.xml
77 lines (72 loc) · 1.99 KB
/
apply.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../../../../template/methodWrapper.xsl"?>
<method name="apply" parent="Function" xmlns="http://www.jsman.net/xsd/manual"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jsman.net/xsd/manual ../../../../schema/jsman-manual.xsd">
<lead>
<p>apply</p>
</lead>
<description>
<p>Function.apply(<params/>)</p>
</description>
<parameters>
<parameter name="this" type="object" optional="false"></parameter>
<parameter name="arguments" type="array" optional="true"></parameter>
</parameters>
<examples>
<example>
<heading>apply example</heading>
<code>
var f = function () {
return this;
};
console.log(f.apply(location.hash)); // Sets a custom this value for the function.
</code>
</example>
<example>
<heading>apply example</heading>
<code>
var getName = function () {
return this.name.first + ' ' + this.name.last;
}
var lars = {
name: {
first: 'Lars',
last: 'Doe'
}
};
console.log(getName.apply(lars));
</code>
</example>
<example>
<heading>apply example</heading>
<code>
var getName = function (lastFirst, includeAge) {
var result = '';
if (lastFirst) {
result = this.name.last + ', ' + this.name.first;
}
else {
result = this.name.first + ' ' + this.name.last;
}
if (includeAge) {
result += ' (' + this.age + ')';
}
return result;
}
var lars = {
name: {
first: 'Lars',
last: 'Doe'
},
age: 20
};
console.log(getName.apply(lars)); // Lars Doe
console.log(getName.apply(lars, [true])); // Doe, Lars
console.log(getName.apply(lars, [false, true])); // Lars Doe (20)
</code>
</example>
</examples>
<external>
<mdn href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function/apply">Function.apply method</mdn>
</external>
</method>