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
...全文
135 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
内容概要:本文提出了一种基于直方图的自动模糊C-均值聚类(FCM)算法,用于遥感图像分割,该方法通过Matlab代码实现。该算法创新性地结合图像灰度直方图信息,自动确定聚类数目和初始聚类中心,有效克服了传统FCM算法对初始参数敏感以及需预先设定聚类数的固有局限。通过深入分析直方图的波峰与波谷分布特征,实现聚类参数的自适应初始化,并融合优化的隶属度矩阵更新策略,从而显著提升了图像分割的精度与算法收敛速度。实验验证表明,该方法在多种复杂的遥感图像数据上均表现出优异的分割效果和强鲁棒性,尤其适用于地物类别未知、分布复杂或背景干扰严重的遥感图像处理任务。; 适合人群:具备一定图像处理基础和Matlab编程能力,从事遥感科学、地理信息系统(GIS)、计算机视觉或模式识别等相关领域研究的研发人员和研究生。; 使用场景及目标:①解决遥感图像自动分割中聚类数目难以预先确定的关键问题;②提升模糊C-均值聚类算法在无监督图像分割中的自动化水平和分割精度;③为缺乏先验知识的实际图像分析任务提供一种高效、可靠的自适应聚类解决方案。; 阅读建议:读者应结合提供的Matlab代码,深入理解直方图分析与FCM算法融合的具体实现步骤,重点关注聚类数自动确定和初始中心选取的逻辑推导过程,并通过实际遥感图像数据进行调试与验证,以全面掌握该方法的核心原理与应用技巧。
内容概要:本文提出了一种基于改进粒子群算法的多无人机协同航迹规划方法,并提供了完整的Matlab代码实现。该方法针对多无人机系统在复杂动态环境中的协同飞行需求,通过改进的粒子群优化(PSO)算法解决航迹规划问题,重点优化路径最优性、飞行安全性及多机协作冲突规避能力。文中系统阐述了算法的设计原理、改进策略及其在多无人机协同场景下的应用流程,通过引入动态障碍物建模与任务约束机制,增强了算法在实际任务中的适应性。仿真实验验证了该方法在降低飞行能耗、提升路径质量、加快收敛速度以及增强系统鲁棒性方面的显著优势,适用于侦察、巡检、灾害救援等多无人机集群任务场景。; 适合人群:具备一定编程基础和智能优化算法知识,从事无人机控制、路径规划、智能调度或群体智能相关研究的研发人员及研究生,尤其适合工作1-3年的科研与工程技术人员。; 使用场景及目标:①解决多无人机在复杂环境下协同避障与高效路径规划问题;②提升传统粒子群算法在高维、多约束航迹规划中的搜索效率与全局寻优能力;③为无人机集群执行多样化任务(如区域覆盖、目标搜索、编队飞行)提供稳定可靠的路径规划解决方案; 阅读建议:建议读者结合所提供的Matlab代码进行仿真实践,按照文档结构逐步理解算法实现细节,重点关注粒子群算法的改进机制、多无人机协同约束建模与适应度函数设计部分,同时可通过调整环境参数与无人机数量深化对算法性能的认知与优化能力。

87,988

社区成员

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

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