HTML代码:
<button id="button1">点击弹出结果1</button>
<button id="button2">点击弹出结果2</button>
<button id="button3">点击弹出结果3</button>
JS代码:
var animal = 'dog';
function getAnimal(adjective) { alert(adjective+' '+this.animal); }
var myObj = {animal: 'camel'};
document.getElementById("button1").onclick = function() {
getAnimal('lovely'); //弹出 'lovely dog'
};
document.getElementById("button2").onclick = function() {
getAnimal.call(myObj, 'lovely'); //弹出 'lovely camel'
};
document.getElementById("button3").onclick = function() {
getAnimal.apply(myObj, ['lovely']); //函数参数以数组形式发送
};