【问题标题】:How do I can access an outside function varible, inside a function in javascript?如何在javascript函数内部访问外部函数变量?
【发布时间】:2017-06-01 01:43:59
【问题描述】:

我的 javascript 中有一个 percent 变量,该变量从 PHP 传递结果。

这是javascript:

test.js

console.log(percent); //* //variable was passed from PHP

function displayLoading() {
    console.log(percent); //**
}

如果我使用 *console.log(percent) out 函数,它将在控制台中打印出percent 的值。但如果我在displayLoading 函数中使用**console.log(percent),它将打印输出为undefined

如何在函数内部访问外部variable

我试过这种方法

来自stackoverflow

var funcOne = function() {
    this.sharedVal = percent;
};
var funcTwo = function() {
    console.log(funcOne.sharedVal);
};

并将 print_out undefined 输入控制台日志。

来自stackoverflow

var per = percents
console.log(per);   //this line print_out the value into console log

function displayLoading() {
   console.log(per);    //this print_out "undefined" into console log.
   var myPercent = per;
   console.log(per);    //and also print_out "undefined" into console log.
}

上面的两个代码都不适合我,有人知道另一种方法吗? 任何帮助表示赞赏,谢谢:)

已编辑:

上面javascript里面的percents,我是从这段代码中得到的:

headerController.php

<?php
   $percent = $percent + 10;
?> 
    <script type="text/javascript">
       var percents = <?= $percent; ?>;
    </script>
    <script type="text/javascript" src="../../web/js/test.js"></script>

主要问题找到了,我之所以得到undefined是因为我在变量从php传递过来之前打印了percents

如何将 php 变量直接传递给 javascript 文件(本例中为 test.js)中的函数?

【问题讨论】:

  • 您在哪个语句中指定百分比值?
  • 也许,您在本地分配变量,尝试在外部分配它然后使用 console.log(window.percent);函数内部
  • I've tried this way - 除了你没有,不是真的
  • 这个小代码sn -p 不足以判断问题。如果 percent 是一个全局变量(看不到它是如何定义的,所以谁知道),那么第一个代码 sn-p 应该可以正常工作
  • 对不起,我已经更新了我的问题。检查一下:)谢谢

标签: javascript php jquery variables scope


【解决方案1】:

定义一个全局变量。

percent = 'foobar';
// window.percent = 'foobar'; /* same thing */

function showthing() {
  console.log(percent);
}

showthing();

【讨论】:

  • 嗯,谢谢。我试过了,它的工作。但是我犯了一个错误,问题原因是当php变量尚未传递给javascript时我调用了该函数。
【解决方案2】:

尝试从var per = percents 中删除var。像这样

per = percents
console.log(per);   //this line print_out the value into console log

function displayLoading() {
   console.log(per);    
   var myPercent = per;
   console.log(per);    
}

【讨论】:

  • 为什么要声明myPercent
  • 不需要。我刚刚复制了你的代码。您所要做的就是将 var per = percents 更改为 per = percents
【解决方案3】:

我不知道你在说哪个控制台(浏览器或 JS REPL - 但它们几乎相同),但 REPL 总是输出函数的返回值。 如上所述,我认为这与全局/本地范围无关。

>> p = 5; function dl() { console.log(p); }
<- 5
>> dl()
   5 
<- undefined
>> foo = function() { return "foo" }
<- function foo()
>> foo()
<- "foo"

【讨论】:

  • 我用的是mozilla日志控制台,就是上面的REPL控制台结果?
  • 这也是来自开发者工具的 mozilla 的 web 控制台。
【解决方案4】:

一个变量:

1) 用var 声明的关键字具有声明它的本地范围。(如果您在函数外部声明它,则不能在函数内部使用它,反之亦然。

例子:

   var  percent = 'foobar';
    function showthing() {
      console.log(percent);//wrong
    }
    console.log(percent);//correct

2) 在特定函数内声明在函数内具有局部作用域。

例子:

     function showthing() {
     var  percent = 'foobar';
      console.log(percent);//correct
    }
console.log(percent);//wrong

【讨论】:

  • 感谢您的知识:)
【解决方案5】:

从 PHP 传递变量的方式是否有问题,比如 AJAX?当您调用 displayLoading() 时,AJAX 调用可能尚未应答。

【讨论】:

  • 是的,你是对的:D。当变量尚未传递时,我调用该函数。可能你知道我如何在 javascript 中直接传递给函数吗?
  • 如果你使用 jQuery,你可以这样做: $.get( "ajax/test.php", function( data ) { displayLoading(); });
  • 谢谢,我只是了解 javascript / jquery。我可以确切地知道我把这段代码放在哪里了吗?在 php 控制器 代码中使用&lt;script&gt;....&lt;/script&gt; 或在我的 test.js?
  • 在您的 javascript 文件中。但我的建议是更改行的顺序而不是添加新代码。在执行 AJAX 的代码块中,获取变量的值后,调用该函数 displayLoading。如果您希望简单地将 JS 与 PHP 结合起来:您的 HTML 文件应该是 PHP 文件,对吗?所以你写在 HTML
猜你喜欢
  • 2017-02-02
  • 1970-01-01
  • 2012-12-31
  • 2020-03-03
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多