【问题标题】:How to get a value of local variable outside the function in java script如何在javascript中获取函数外部的局部变量值
【发布时间】:2020-08-05 13:26:20
【问题描述】:

Javascript 新手 - 如何在员工对象之外打印“sam”的值

<html>
      <head>
       <script>
         var employee = {
            empname: "David",
            department : "Finance",
            id : 002,
            details : function() {
                
                this.empname = "Sam";
               return this.empname + " with Department " + this.department;
            }
         };

         document.write(employee.empname);
         </script>
       </head>
    </html>

提前致谢!!

【问题讨论】:

  • 你必须调用详情函数employee.details()

标签: javascript function object


【解决方案1】:

在 details 方法中,您正在将 empname 的值更新为 Sam,您需要调用该方法然后使用它。

<html>
      <head>
       <script>
         var employee = {
            empname: "David",
            department : "Finance",
            id : 002,
            details : function() {
                
                this.empname = "Sam";
               return this.empname + " with Department " + this.department;
            }
         };
         employee.details()
         document.write(employee.empname);
         </script>
       </head>
    </html>

【讨论】:

  • 很高兴听到它对您有所帮助。我希望您不会介意接受答案以获得更好的知名度。
  • 你也可以投票赞成我的问题。如果您发现这是一个适合学习者(新生)的好问题
猜你喜欢
  • 1970-01-01
  • 2020-01-11
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多