function test(a, b, c) {}
console.log(test.length); // 3
function test(a, b = 1, c) {}
console.log(test.length); // 1
function test(a, b, c) {
console.log(arguments.length); // 2
}
test(1, 2);
arguments.length
function test(a, b, c) {
arguments[1] = 3;
console.log(b); // 3
}
test(1, 2);
指定函数的默认值后:
function test(a, b = 5, c) {
arguments[1] = 3;
console.log(arguments); // [1, 3]
console.log(b); // 2
}
test(1, 2);
函数内部更改实参的值,能够更改实参,但是对形参没有任何影响
function foo({x, y = 5}) {
console.log(x, y);
}
foo({}); // undefined 5
foo({x: 1}); // 1 5
foo({x: 1, y: 2}); // 1 2
foo(); // 报错 因为他没有属性则没有包装类,匹配不上{},所以报错
上述不传值的解决办法:
function foo({x, y = 5} = {}) { // 解构赋值依然有默认值 {}
console.log(x, y);
}
// 参数是undefined的时候会找默认值为空对象的默认值,从而达到匹配
foo(); // undefined 5
// 简写方式
function fetch(
url,
{
body = "",
method = "GET",
header = {}
}
) {
console.log(method);
}
// 完整方式
function fetch(
url,
{
body: body = "",
method: method = "GET",
header: header = {}
}
) {
console.log(method); // GET
}
fetch('http://www.baidu.com', {});
调用fetch的时候,不想手动传第二个参数,那么我们就可以给他的解构赋值赋一个默认值:
function fetch(
url,
{
body = "",
method = "GET",
header = {}
} = {}
) {
console.log(method);
}
fetch('http://www.baidu.com'); // GET
var a = 1;
var a = 2; // var存在变量提升,可以重新赋值,覆盖
console.log(a); // 2
var x = x; // x声明到全局,没有赋值
console.log(x); // undefined
let b = 1;
let b = 2; // let不存在变量提升,不可以重新赋值,报错
let c = c; // c没有声明 报错 c is not defined
// x还没有定义,不可以在初始化之前调用x
function foo(x = x) { // Cannot access 'x' before initialization
}
foo();
var x = 1;
// 当给函数默认值的时候,就已经给了一个块级作用域 let x,y
function foo(x, y = function() {
x = 2;
console.log(x); // 2
}, z = function() {
console.log(x);
}) {
var x = 3;
y(); // 2
console.log(x); // 3
z(); // 1
}
foo();
console.log(x); // 1
a();
function a () {
console.log(this); // window
}
a();
function a () {
'use strict';
console.log(this); // undefined
}
function foo() {
console.log(this.a);
}
var obj = {
a: 2,
foo: foo
}
obj.foo(); // 2
var bar = obj.foo;
bar(); // window.bar(); -> window上没有,则为undefined
在new的时候将this转变成实例化的对象
let f = a => {
return a;
}
// 相当于
let f = function(a) {
return a;
}
let f = a => a;
// 相当于
let f = function(a) {
return a;
}
let f = () => a;
// 相当于
let f = function() {
return a;
}
let f = function() {
let a = 1,
b = 2;
console.log(a + b);
}
// 函数参数的解构赋值
const full = ({f, l} = {}) => f + '' + l;
function full({f, l} = {}) { // 相当于
return f + '' + l;
}
console.log(full({f: 'l', l: 'yb'})); // lyb
var sum = (a, b) => {
console.log(arguments); // arguments is not defined
}
sum();
=>
跟function
本质上是两个东西,不是function
的简写// args: 随便定义,但是我们习惯使用args
var sum = (...args) => {
console.log(args); // [1, 2]
}
sum(1, 2);
let fn = (a, b, ...c) => {
console.log(a, b, c);
}
// let fn = (...c, a, b) => { // 报错
// console.log(a, b, c);
// }
fn(1, 2, 3, 4, 5, 6); // 1 2 [3, 4, 5, 6]
function foo(a, b, c) {
console.log(a, b, c);
}
foo(...[1, 2, 3]); // 1 2 3
function foo(a, b, c) {
console.log(a, b, c);
console.log(this);
}
foo.apply(null, [1, 2, 3]); // 1 2 3 window
foo.apply(undefined, [1, 2, 3]); // 1 2 3 window
let a = [1, 2, 3];
let b = [0, ...a, 4];
console.log(b); // [0, 1, 2, 3, 4]
console.log((function(a){}).length); // 1
console.log((function(...a){}).length); // 0
console.log((function(a, b, ...c){}).length); // 2
console.log((function(a, b = 1, ...c){}).length); // 1