Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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中在一个函数中声明全局变量赋值并在另一个函数中使用_Javascript_Global Variables - Fatal编程技术网

如何在javascript中在一个函数中声明全局变量赋值并在另一个函数中使用

如何在javascript中在一个函数中声明全局变量赋值并在另一个函数中使用,javascript,global-variables,Javascript,Global Variables,我只是编写了一个测试html文件来了解javascript中的对象。代码如下 脚本内标记 <script type="text/javascript"> var obj = new ParentFn(); var obj2 = new AnotherParentFn(); var temp; function initer() { temp = obj.Adding(); obj2.caller();

我只是编写了一个测试html文件来了解javascript中的对象。代码如下

脚本内标记

<script type="text/javascript">

    var obj = new ParentFn();
    var obj2 = new AnotherParentFn();
    var temp;
    function initer()
    {
        temp = obj.Adding();
        obj2.caller();
    }
    function ParentFn()
    {
        this.a = 10;
        this.b = 20;
    }
    function AnotherParentFn()
    {
        this.a = 30;
        this.b = 50;
    }
    AnotherParentFn.prototype.caller = function()
    {
        var self = this;
        temp();
    }
    ParentFn.prototype.Adding = function()
    {
        var self = this;
        document.getElementById("id_div1").innerHTML = " Method Called and Result of a+b is " + (self.a + self.b);          
    }

</script>

var obj=新的ParentFn();
var obj2=新的另一个parentfn();
无功温度;
函数initer()
{
temp=obj.Adding();
obj2.caller();
}
函数ParentFn()
{
这个a=10;
这个b=20;
}
函数另一个父函数fn()
{
这个a=30;
这个b=50;
}
AnotherParentFn.prototype.caller=函数()
{
var self=这个;
温度();
}
ParentFn.prototype.Adding=函数()
{
var self=这个;
document.getElementById(“id_div1”).innerHTML=“调用的方法和a+b的结果是”+(self.a+self.b);
}
在我使用的身体里

<button onclick="initer()"> Click here to test </button>
<div id="id_div1"></div>
单击此处进行测试
问题是当从initer()函数调用另一个parentfn.prototype.caller时,temp变量仍然未定义。代码有什么问题


我的任务是为函数ParentFn.prototype.Adding分配一个全局变量,并从另一个ParentFn.prototype.caller函数调用该全局变量。如何实现

通过写入
temp=obj.Adding()它存储返回值。
temp
中没有函数指针。用这个

function initer()
{
    temp = obj.Adding;
    obj2.caller();
}

通过写入
temp=obj.Adding()它存储返回值。
temp
中没有函数指针。用这个

function initer()
{
    temp = obj.Adding;
    obj2.caller();
}

括号用于执行函数。 将值分配给
temp
时,调用函数并将结果(
undefined
)分配给
temp
。要在
temp
中存储对函数的引用,请省略括号

temp = obj.Adding;

括号用于执行函数。 将值分配给
temp
时,调用函数并将结果(
undefined
)分配给
temp
。要在
temp
中存储对函数的引用,请省略括号

temp = obj.Adding;

首先,对
obj.Adding
的引用分配不正确;应该是这样(不带括号):

然后,在
另一个parentfn.prototype.caller
自身内部,您必须在调用过程中使用以下命令将当前对象作为
this
显式传递:


首先,对
obj.Adding
的引用分配不正确;应该是这样(不带括号):

然后,在
另一个parentfn.prototype.caller
自身内部,您必须在调用过程中使用以下命令将当前对象作为
this
显式传递:


您不需要将其保存为全局变量。它已经保存在
ParentFn.prototype
中。您所需要做的就是使用
调用它。调用
并传入所需的接收器。您可以像这样实现另一个parentfn.prototype.caller

AnotherParentFn.prototype.caller = function()
{
    ParentFn.prototype.Adding.call(this);
}

这样你就可以完全摆脱
temp
。您也不需要将
分配给任何地方的本地
var self

您不需要将其保存为全局变量。它已经保存在
ParentFn.prototype
中。您所需要做的就是使用
调用它。调用
并传入所需的接收器。您可以像这样实现另一个parentfn.prototype.caller

AnotherParentFn.prototype.caller = function()
{
    ParentFn.prototype.Adding.call(this);
}

这样你就可以完全摆脱
temp
。您也不需要将
分配给任何地方的本地
var self

您的临时变量现在已经是全局变量,如果您可以接受任何答案,那就太好了。您的临时变量现在已经是全局变量,如果您可以接受任何答案,那就太好了。