<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>实验</title>
</head>
<body>
<script type="text/javascript">
//解释 forEach(function(currentValue, index, arr), thisValue)
//中的 thisValue是怎么回事,结合this召唤引用对象;
//菜鸟教程:http://www.runoob.com/jsref/jsref-foreach.html
//解释详情 https://www.cnblogs.com/joyco773/p/6113748.html
var obj = {
showResults: function(value, index) {
var squared = this.calcSquare(value);
document.write("value: " + value);
document.write(" index: " + index);
document.write(" squared: " + squared);
document.write("<br />");
},
calcSquare: function(x) { return x * x }
};
var numbers = [5, 6];
//1行
numbers.forEach(obj.showResults, obj);
//2行 1行2行代码等效
numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);
// Output:
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>实验</title>
</head>
<body>
<script type="text/javascript">
//解释 forEach(function(currentValue, index, arr), thisValue)
//中的 thisValue是怎么回事,结合this召唤引用对象;
//菜鸟教程:http://www.runoob.com/jsref/jsref-foreach.html
//解释详情 https://www.cnblogs.com/joyco773/p/6113748.html
var obj = {
showResults: function(value, index) {
var squared = this.calcSquare(value);
document.write("value: " + value);
document.write(" index: " + index);
document.write(" squared: " + squared);
document.write("<br />");
},
calcSquare: function(x) { return x * x }
};
var numbers = [5, 6];
//1行
numbers.forEach(obj.showResults, obj);
//2行 1行2行代码等效
numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);
// Output:
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
</script>
</body>
</html>