Copying, passing, and comparing by value 的 疑惑

moliu 2009-10-21 08:00:14
var n = 1;
var m = n;
function add_to_total(total, x)

{

total = total + x;
document.write(total+"</br>");
document.write(x+"</br>")
}



add_to_total(n, m);//输出 2 和 1,
document.write(n+"</br>")//输出 1
document.write(m+"</br>")//输出 1
// 后面讲解中,提到 n=1 m=2 ,可上面的程序证明,却不是这样。请达人讲解,谢谢

附:js权威指南中的讲解
var n = 1; // Variable n holds the value 1

var m = n; // Copy by value: variable m holds a distinct value 1



// Here's a function we'll use to illustrate passing by value

// As we'll see, the function doesn't work the way we'd like it to

function add_to_total(total, x)

{

total = total + x; // This line changes only the internal copy of total

}



// Now call the function, passing the numbers contained in n and m by value.

// The value of n is copied, and that copied value is named total within the

// function. The function adds a copy of m to that copy of n. But adding

// something to a copy of n doesn't affect the original value of n outside

// of the function. So calling this function doesn't accomplish anything.

add_to_total(n, m);



// Now, we'll look at comparison by value.

// In the following line of code, the literal 1 is clearly a distinct numeric

// value encoded in the program. We compare it to the value held in variable

// n. In comparison by value, the bytes of the two numbers are checked to

// see if they are the same.

if (n == 1) m = 2; // n contains the same value as the literal 1; m is now 2
...全文
142 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
moliu 2009-10-29
  • 打赏
  • 举报
回复
if (n == 1) m = 2; // n contains the same value as the literal 1; m is now 2
//n 值确实 为 1 ,给 m 重新赋值 2,m is now 2 。原来 m 值 为 1 。
//原来是这样理解的,以为 m 的值为 2 ,是函数执行后的结果。这样的理解是错误的。
moliu 2009-10-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fandelei1982 的回复:]
if (n == 1) m = 2;  // n contains the same value as the literal 1; m is now 2

[/Quote]
还是不明白。为什么 m=2 ?
moliu 2009-10-21
  • 打赏
  • 举报
回复
var n = 1;
var m = n;
function add_to_total(total, x)

{

total = total + x;
document.write(total+" </br>");
document.write(x+" </br>")
}
add_to_total(n, m);//输出 2 和 1
//这如何解释?
friendly_ 2009-10-21
  • 打赏
  • 举报
回复

if (n == 1) m = 2; // n contains the same value as the literal 1; m is now 2
内容概要:本文围绕大功率维也纳整流器的高性能控制问题,提出了一种复合调制策略与多闭环协同控制方法,并通过Simulink平台进行了系统建模仿真与性能分析。研究聚焦于高压、大功率应用场景下维也纳(Vienna)整流器的关键技术挑战,如功率因数提升、输入电流谐波抑制、输出电压稳定控制以及中点电位平衡等。文中详细设计了基于马鞍波SPWM的复合调制算法,以提高直流母线电压利用率并优化谐波分布;同时构建了包含电压外环、电流内环、无功补偿环及中点电位平衡环在内的多闭环协同控制系统,实现对有功、无功功率的精确解耦控制与动态响应优化。通过Simulink搭建完整的三相维也纳整流器仿真模型,对不同工况下的系统启动、负载突变、电网扰动等情况进行全面仿真测试,验证了所提控制策略在提升系统效率、增强抗干扰能力、改善电能质量等方面的优越性能。; 适合人群:具备电力电子与电力传动基础知识,从事新能源发电、工业电源、电动汽车等领域研究或开发工作的研究生、工程师及科研人员;熟悉Simulink仿真工具者更佳。; 使用场景及目标:①掌握维也纳整流器的工作原理及其在高效率AC-DC变换中的应用;②学习复合调制技术(如马鞍波SPWM)的设计思路与实现方法;③深入理解多闭环协同控制架构在复杂电力电子系统中的构建逻辑与参数整定技巧;④通过仿真实践提升对功率因数校正、谐波抑制、中点电位平衡等关键问题的分析与解决能力; 阅读建议:建议读者结合Simulink模型同步操作,边学边练,重点关注控制环路的搭建逻辑、调制信号的生成方式及各状态变量的动态响应特性,同时可尝试调整系统参数以观察性能变化,从而加深对控制策略本质的理解。

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧