forked from lfthwjx/effective-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
45 lines (39 loc) · 1.32 KB
/
demo.js
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
// 尝试修改arguments对象
function callMethod(obj, method) {
var shift = [].shift;
shift.call(arguments);
shift.call(arguments);
console.log(arguments); // { '0': 1, '1': 2 }
console.log(arguments[0] === obj, arguments[0] === 1); // true true(当我们的第三个参数是数字1)
console.log(arguments[1] === method, arguments[1] === 2); // true true(当我们的第四个参数是数字2)
// 因为obj === arguments[0]
// 因为method === arguments[1]
return obj[method].apply(obj, arguments); // 相当于[]
}
var obj = {
add: function(x, y) {
return x + y;
}
};
//callMethod(obj, 'add', 1, 2); // Cannot read property 'apply' of undefined
// 严格模式下
function strictMode(x) {
'use strict';
arguments[0] = 'something';
console.log(x, arguments[0]); // hello something
return x === arguments[0];
}
// 非严格模式下
function notStrictMode(x) {
arguments[0] = 'something';
console.log(x, arguments[0]); // something something
return x === arguments[0];
}
console.log(strictMode('hello')); // false
console.log(notStrictMode('hello')); // true
// 修复刚开始的方法
function fixCallMethod(obj, method) {
var args = [].slice.call(arguments, 2);
return obj[method].apply(obj, args);
}
console.log(fixCallMethod(obj, 'add', 1, 2)); // 3