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
...全文
126 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
内容概要:本文档聚焦于基于Peng-Robinson状态方程的化工热力学计算研究,系统介绍了利用Matlab代码实现纯组分及多组分系统的压缩因子(z因子)和逸度系数的计算方法,并进一步拓展至泡点压力与露点压力的数值求解。资源涵盖了从状态方程推导、非线性方程求根、相平衡条件判断到迭代算法实现的完整技术路线,提供了可运行的Matlab程序代码,有助于深入理解实际工程中物性参数的计算原理与实现过程; 适合人群:具备化工热力学基础知识和一定Matlab编程能力的高校学生、科研人员及工程技术人员,尤其适合从事过程系统模拟、油气田开发、精馏设计及相关领域研究的专业人士; 使用场景及目标:① 掌握Peng-Robinson方程在真实气体物性计算中的应用;② 实现多组分混合体系的相平衡计算与泡/露点预测;③ 为Aspen Plus等商用流程模拟软件提供底层算法验证与教学参考;④ 支持科研论文复现、课程设计、毕业设计及工业过程优化中的热力学数据支撑需求; 阅读建议:建议读者结合经典化工热力学教材同步学习,重点理解立方型状态方程的根判别、迭代收敛策略与相平衡准则的数学表达,动手调试并运行所提供的Matlab代码以掌握算法细节,还可将其扩展应用于其他状态方程(如SRK、vdW)的对比分析研究。
内容概要:本文围绕列车-轨道-桥梁交互仿真研究,基于Matlab平台构建数值模型,系统分析列车运行过程中轨道与桥梁结构间的动态相互作用机制。研究涵盖多体动力学建模、耦合系统运动方程求解、边界条件设定及仿真结果可视化等关键环节,重点揭示高速行车条件下基础设施的振动传递规律与力学响应特征。该仿真方法可有效评估结构安全性、舒适性指标及疲劳寿命,为轨道交通工程的设计优化与运维管理提供理论支撑和技术路径。文中配套提供了完整的Matlab代码实现方案及操作说明,便于用户复现、验证和拓展相关研究。; 适合人群:具备Matlab编程基础和结构动力学、车辆动力学等相关专业知识的研究生、科研人员及从事铁路工程、桥梁工程与交通系统安全评估的工程技术人才,尤其适合开展轨道交通耦合振动课题的研究者。; 使用场景及目标:①用于高校与科研机构进行列车-轨道-桥梁耦合系统动力学特性的教学演示与科学研究;②支撑高速铁路桥梁的设计优化、运营安全性评估与减振降噪方案验证;③为复杂交通基础设施的多物理场耦合仿真提供建模思路与代码参考。; 阅读建议:建议读者结合所提供的Matlab代码逐模块深入研读,重点关注系统建模假设、质量-刚度-阻尼矩阵构建方法及数值积分算法的实现细节,同时可通过调整参数进行敏感性分析,进一步掌握仿真模型的适用范围与优化方向。

87,989

社区成员

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

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