<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Call Function Study</title>
<script type="text/javascript" charset="utf-8">
function sum(a,b) {
return a+b;
}
function call1(a,b) {
return sum.call(this,a,b);
}
function apply1(a,b) {
return sum.apply(this,[a,b]);
}
window.color = 'red';
var obj1 = {color:'blue'};
function showColor() {
document.write(this.color);
document.write("<br>");
document.write("<br>");
}
document.write(call1(10,20));
document.write("<br>");
document.write(apply1(100,20));
document.write("<br>");
document.write("<br>");
document.write("<br>");
showColor.call(this);
showColor.call(window);
showColor.call(obj1);
document.write("<br>");
document.write("<br>");
document.write("<br>");
//模拟call方法
function test1(a,b,c) {
return a+b+c;
}
function Obj1(a,b,c) {
this.a = a;
this.b = b;
this.c = c;
return a-b-c;
}
var o = new Obj1(2,2,3);
document.write(o + ' type: ' + (typeof o) + ' ' + o.a);
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write(test1.call(o, o.a, o.b, o.c));
o.method = test1;
document.write("<br>");
document.write("<br>");
document.write("<br>");
document.write(o.method(o.a, o.b, o.c));
delete o.method;
</script>
</head>
<body>
</body>
</html>
学习资料: http://www.icoolxue.com/play/9358