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
...全文
144 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
内容概要:本文研究了一种周期感知LightGBM预测与NSGA-Ⅱ优化耦合的建筑群能源系统协同调度方法,旨在提升建筑群能源系统的低碳经济性和运行效率。该方法首先利用周期感知的LightGBM模型对建筑群的能源负荷与可再生能源出力进行高精度时序预测,充分挖掘数据中的周期性规律,显著提高预测准确性;随后,结合多目标优化算法NSGA-Ⅱ,构建协同调度模型,综合考虑经济成本、碳排放和能源利用率等多个目标,求解得到一组帕累托最优调度方案,实现多目标间的均衡优化。研究通过Python编程实现了完整的预测与优化流程,并在实际场景中验证了所提方法在降低系统运行成本和减少碳排放方面的有效性。; 适合人群:具备一定Python编程基础,从事电力系统、能源管理、智能优化或机器学习相关领域的研究人员与工程技术人员,尤其适合关注多目标优化与时间序列预测融合应用的硕士、博士研究生及科研工作者。; 使用场景及目标:①应用于建筑群、园区等多主体能源系统的协同调度与低碳优化;②实现对光伏、负荷等具有强周期性特征的时间序列数据进行精准预测;③解决能源调度中经济性、环保性等多目标相互冲突的优化问题,获得兼顾各方需求的调度策略。; 阅读建议:此资源以实际代码实现为核心,建议读者在学习过程中结合Python代码,重点理解周期特征提取、LightGBM模型构建、NSGA-Ⅱ算法应用及多目标优化结果分析等关键环节,并尝试在不同场景下复现和调整参数,以深化对方法原理与工程应用的理解。

87,984

社区成员

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

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