javascript指针功能

xde*_*000 0 javascript pointers function

如果我有:

function init(t,y,u) 
{
   alert(t + " " + y + " " + u);
}

// String.prototype.add = init(5, 6, 7);  // 1)   
// window.onload = init(5,6,7); // 2)
Run Code Online (Sandbox Code Playgroud)

在1)init中将执行然后它指针被指定String.prototype.add 但在2)该函数只执行一次...但为什么不是两次也onload引发事件?

谢谢

Kon*_*lph 8

在1)init将被执行,然后它指针被声明为String.prototype.add

不,不会.该函数将被简单地执行,并将返回其返回值(undefined)String.prototype.add.不会分配任何函数指针.为此,您需要返回一个函数!

function init(t,y,u) {
    alert(t + " " + y + " " + u);
    return function () { alert('function call!'); };
}
Run Code Online (Sandbox Code Playgroud)


Joh*_*ith 5

您可能需要以下内容:

String.prototype.add = function () { init(5, 6, 7); };
Run Code Online (Sandbox Code Playgroud)