日期:2014-05-16 浏览次数:20322 次
call([thisObj] [,arg1][, arg2][,...][,argN]) thisObj 可选项。将被用作当前对象的对象。 arg1, arg2, , argN 可选项。将被传递方法参数序列。
foo.call(thisObj, arg1,arg2,arg3) == thisObj.foo(arg1, arg2, arg3)
<input type="button" name="na_but" id="id_but" value="测试call" onclick="testFun()"/> <script type="text/javascript"> var value = "Global Object"; var testFun = function () { /** 没有传递参数 */ window.printValue(); //Global Object printValue.call(window); //Global Object printValue.call(document.getElementById("id_but")); //测试call printValue.call(new objCall()); //call Object testObj.call(this, "hello", "world", "luchunli"); //调用构造方法 } function objCall () { this.value = "call Object"; } function printValue () { alert(this.value); } function testObj () { var str = ""; for(var j = 0 ,end = arguments.length ; j < end ; j++){ str += arguments[j]; } alert(str); //helloworldluchunli } </script>
<input type="button" name="na_but" id="id_but" value="测试call" onclick="testFun()"/> <script type="text/javascript"> var value = "Global Object"; var testFun = function () { //value : Global Object str : Hello Window printValue.call(window, "Hello Window"); //value : call Object str : Hello printValue.call(new objCall(), "Hello"); //value : 测试call str : input printValue.call(document.getElementById("id_but"), "input"); } function objCall () { this.value = "call Object"; } function printValue (str) { var value = "print value"; alert("value : " + this.value); alert("str : " + str); } </script>
<input type="button" name="na_but" id="id_but" value="测试call" onclick="testFun()"/> <script type="text/javascript"> var value = "Global Object"; var message = "Global Message"; var testFun = function () { var b = new objB(); var a = new objA(); b.setMessage.call(a, "a的消息"); //==>a.setMessage('a的消息'); //message : a的消息; str : This is a; value : call ObjectA printMessage.call(a, "This is a"); } function objA () { this.value = "call ObjectA"; this.setMessage = setMessage; } function objB () { this.value = "call ObjectB"; this.setMessage = setMessage; } function printMessage (str) { alert("message : " + this.message + ";\tst