Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Plus API onload回调行为_Javascript_Google Plus_Google Plus One - Fatal编程技术网

Javascript Google Plus API onload回调行为

Javascript Google Plus API onload回调行为,javascript,google-plus,google-plus-one,Javascript,Google Plus,Google Plus One,我试图从我的对象中的函数中加载Google Plus API,如: var myObject = { initialize : function(options) { window.___gcfg = { parsetags : 'explicit' }; (function() { var po = document.createElement('script');

我试图从我的对象中的函数中加载Google Plus API,如:

var myObject = {
    initialize : function(options) {
        window.___gcfg = {
          parsetags : 'explicit'
        };  

        (function() {
            var po = document.createElement('script');
            po.type = 'text/javascript';
            po.async = true;
            po.src = 'https://apis.google.com/js/plusone.js?onload=onLoadGooglePlus';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(po, s);
        })();

        ...
然后我在对象外部定义了onLoadGooglePlus,作为一个常规的全局函数

现在,如果我执行匿名函数从我的对象中加载GAPI,则使用初始化函数不会调用回调。在本例中,this关键字指向我的对象

但是,如果我执行以下操作,使this关键字指向窗口,它确实可以工作:

initialize : function(options) {
    window.___gcfg = {
      parsetags : 'explicit'
    };  

    setTimeout(function() {
        (function() {
            var po = document.createElement('script');
            po.type = 'text/javascript';
            po.async = true;
            po.src = 'https://apis.google.com/js/plusone.js?onload=onLoadGooglePlus';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(po, s);
        })();
    }, 0);
...

我真的很困惑,这和这个关键词有关系吗?因为从Firefox的控制台调用我的函数myObject.initialize可以使其工作,所以执行onload回调,但如果不使用setTimeout…,则不会执行onload回调,0

如果我在窗口对象中设置函数,例如window.onLoadGooglePlus=函数{…},它也可以工作