<html>
<body>
<script>
var arry=[1,2,3,4,5,6];
alert((function(){
for(var i=0,ret=arry[0]; i<arry.length; i++)
{
ret = Math.max(ret,arry[i])
}
return ret;
})());
alert((function(){ return Math.max.apply(null,arry); })())
</script>
<input type="button" id="btnSubmit" value="Click me!" />
<script language="javascript" type="text/javascript">
function ButtonManager(buttonId, message)
{
debugger;
this._message = message;
document.getElementById(buttonId).onclick = createDelegate(this,this.ShowMessage); //结果:hello 原因:通过function.call()方法,把当前的函数指针赋给ShowMessage
//document.getElementById(buttonId).onclick = this.ShowMessage; //结果:undefined 原因:此时ShowMessage里面的this对象window对象
}
ButtonManager.prototype.ShowMessage=function()
{
alert(this._message);
}
function createDelegate(object, method)
{
var delegate = function()
{
method.call(object);
}
return delegate;
}
var btn = new ButtonManager("btnSubmit", "Hello!");
var x=10;
var o ={x:15};
function foo()
{
alert(this.x);
}
foo();
foo.call(o);
</script>
</body>
</html>
补充:当使用了fn.call(obj)方法以后,fn方法中的this所指向的对象即为obj对象。
//***********************************//
function classA()
{
alert(this.msg);
}
function classB(name,msg)
{
alert(name);
this.msg = msg;
classA.call(this);
}
classB("kro","我叫什么?")
//***********************************//