JS实用篇复习笔记(5)

235 阅读1分钟

1、函数 Function

  • 函数 声明
function showMessage() { alert( 'Hello everyone!' ); }

1、局部变量

function test() {
let message = "Hello, I'm test!"; // local variable
  alert( message );
}

test(); // Hello, I'm test!

alert( message ); // <-- Error! The variable is local to the function

2、外部变量

函数也可以访问外部变量 外部变量仅在没有本地变量时才使用 先查找内部变量 然后查找外部变量

let userName = 'John';

function test() {
  let message = 'hi, ' + userName;
  alert(message);
}

test(); // hi, John

并且 可以修改 外部变量

let userName = 'John';

function test() {
  userName = "Bob"; // (1) changed the outer variable

  let message = 'hi, ' + userName;
  alert(message);
}

alert( userName ); // John before the function call

test();

alert( userName ); // Bob, the value was modified by the function

3、使用参数将任意数据传递给函数 在下面的示例中,该函数有两个参数:datatext

function showMessage(data , text) { // parameters: data, text
  alert(data + ': ' + text);
}

showMessage ('test', 'Hello!'); // test: Hello!
showMessage ('test1', "test"); // test1:test 

4、可给函数参数默认值

function showMessage(data, text = "no text given here" ) {
  alert( data + ": " + text );
}

showMessage("data"); // data: no text given here

5、这个是有用的思想 参数检查 我们可以通过将其与undefined以下内容进行比较来检查该参数是否在函数执行期间被传递: 有提示作用

function test(text) {
  // ...

if (text === undefined) { // if the parameter is missing
    text = 'empty message';
  }

  alert(text);
}

test(); // empty message

6、函数 返回值 函数可以将一个值作为结果返回到调用代码中

一个有空return或没有它的函数返回undefined

  • 注意 永远不要在return和 值之间添加换行符
return
 (some + long + expression + or + whatever * f(a) + f(b))

这不起作用,因为 JavaScript 在return. 这将与以下内容相同:

return ; 
 (some + long + expression + or + whatever * f(a) + f(b))
  • 那如果 需要展示多行 怎么做
return ( 
something do at this part ...
)

7、重要的部分 函数命名 规范

  • "get…" – 返回一个值,
  • "calc…" – 计算一些东西,
  • "create…" – 创造一些东西,
  • "check…" – 检查一些东西并返回一个布尔值等。
  • 很短的函数命名 不常用 但需要注意 jQuery框架定义了一个带有$. 该Lodash核心功能命名_

常规 可以省略掉 else 则 省略掉

// 这个比较初级的写法
function checkAge(age) {
if (age > 18) { return true; } 
else { 
// ... return confirm('right?'); }
}

//高级的写法 
function checkAge(age) {
  if (age > 18) {
    return true;
  }
// ...
  return confirm('Did parents allow you?');
}
  • 一个简单的问题 怎么写比较好 ? 编写一个函数min(a,b),它返回两个数字中的最小值ab 使用的解决方案if
function min(a, b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

带有问号运算符的解决方案'?'

function min(a, b) {
  return a < b ? a : b;
}

2、 函数表达式

1、另外一种创建函数的方法 它看起来像这样:

let sayHi = function() {
  alert( "Hi" );
};

2、有意思的简单交互函数 该函数应该询问question和,根据用户的回答,调用yes()no()

function ask(question, yes, no) {
  if (confirm(question)) yes()
  else no();
}

function showOk() {
  alert( "agreed." );
}

function showCancel() {
  alert( "canceled" );
}

ask(" it?", showOk, showCancel);

3、函数表达式和 函数声明之间的区别是什么 ?

函数表达式在执行到达时创建,并且仅从那一刻起可用

  • 但函数 可在声明前被调用
  • 因为 函数声明会在 执行前 提升到 顶部 例如
sayHi("test"); // Hello, test

function sayHi(name) {
  alert( `Hello, ${name}` );
}
  • 需要声明一个函数时,最好使用函数声明,因为它在声明本身之前是可见的,但函数声明有块作用域

3、箭头函数

let func = (arg1, arg2, ..., argN) => expression

换句话说,它是以下的较短版本:

let func = function(arg1, arg2, ..., argN) {
  return expression;
};
  • 进一步简化 只有一个参数 前面括号省略 只有一个返回值 后面 括号省略

  • 多行 内容 需要 放在 花括号内