English 中文(简体)
作为javascript中的功能性论点的标语
原标题:primitives as function arguments in javascript
  • 时间:2012-05-09 00:13:08
  •  标签:
  • javascript

我在这里学习联合材料,并在作为论点通过时,对原始价值提出了问题。 Saye 我的职能很简单:

var first = 5;
var second = 6;

function func(){
    first+=second;
}
func();
alert(first); //outputs 11

因此,在这种情况下,第一种数值为11。 但是,如果我尝试先把它作为行使职能的理由,则首先仍然是5个。

var first = 5;
var second = 6;

function func(first){
    first+=second;
}
func(first);
alert(first); //outputs 5

wondering if someone could explain this to me.

最佳回答

这是因为,当你打电话<条码>第1条(时,它没有理由使用全球“第一”一词。 但是,当你打电话<代码>第一(第一)时,你说浏览器,现在首先是一个地方变量(仅在职能范围内),而且对全球的“瓦尔<代码>第1

var first = 5;
var second = 6;
function func(first){
    first += second; //Local var "first" + global var "second"
    alert(first); //Local var, outputs 11
}
func(first);
alert(first); //Global var, outputs 5
问题回答

Due to Javascript s scoping, the scoped variable first is overriding the global variable first. In other words, you re adding second to the scoped version of first inside your function (which is scoped because you re declaring it inside the function), and the global version of first is unaffected.

如果你将<条码>第1的声明从该功能中删除, Java则将下一个版本的<条码>第1移至此处,就可找到全球版本。

var first= 5;
var second= 6;

function func(first){

first+=second; // change the local first variable

}
func(first);
alert(first);//outputs 5 - the outer variable wasn t changed.

var first= 5; // global var
var second= 6;

function func(){ // first wasn t defined in the inner scope.

first+=second; // change the global bar

}
func();
alert(first);//outputs 11 - the outer variable was changed.

马克·林图斯是你的答案,但我要补充一个重要的说明。 Java的主要价值总是以价值通过,而阿雷拉和目标总是通过参考。 例:

var first = [  a ,  b  ];

function func( arr ) {
    arr.push(  c  );
}

func( first );

alert( first.join(  ,  )); // output is  a,b,c 

如果在物体或射线上有物体,则会变得更加复杂。 只是记住,改变将影响到你方案的所有情况。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签