JS Functions

Calling a Function with Arguments

send as many arguments as you like, separated by commas (,) 可通过函数进行参数传递


Functions With a Return Value

When using the  return  statement, the function will stop executing (not the entire JavaScript that will stop executing), and return the specified value.

遇到return关键字停止函数执行,返回一个特定的值。

You can also use the return value without storing it as a variable, make a return value based on arguments passed into the function

可以使用返回值而不需要存储它,函数返回值可以是基于传递参数。

function myFunction(a,b)
{
  return a*b;
}
document.getElementById("demo").innerHTML= myFunction(4,3);

The return statement is also used when you simply want to exit a function. 使用return关键字退出函数执行

function myFunction(a,b)
{
  if (a>b)
  {
    return;
  }
  x=a+b
}

The Lifetime of JavaScript Variables

The lifetime of JavaScript variables starts when they are declared. JS变量的生命周期开始于声明

Local variables are deleted when the function is completed. 当函数执行完毕,局部变量被删除

Global variables are deleted when you close the page. 当关闭页面,全局变量被删除


注意:

If you assign a value to a variable that has not yet been declared, the variable will automatically be declared as a GLOBALvariable, even if it is executed inside a function.

如果给一个未声明的变量赋值,这个变量将自动成为全局变量,即使它是在一个函数里。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值