, JavaScript .
JavaScript . ?
JavaScript, .
, , , . , : / , .
name
.
, name:
function sayHi() {
alert("");
}
alert(sayHi.name); // sayHi
, name . , , , :
let sayHi = function() {
alert("");
};
alert(sayHi.name); // sayHi ( !)
, :
function f(sayHi = function() {}) {
alert(sayHi.name); // sayHi (!)
}
f();
. , .
:
let user = {
sayHi() {
// ...
},
sayBye: function() {
// ...
}
}
alert(user.sayHi.name); // sayHi
alert(user.sayBye.name); // sayBye
. , . , :
//
let arr = [function() {}];
alert( arr[0].name ); // < >
// JavaScript ,
, , .
length
length, , :
function f1(a) {}
function f2(a, b) {}
function many(a, b, ...more) {}
alert(f1.length); // 1
alert(f2.length); // 2
alert(many.length); // 2
, , rest , .
, ask question - handler.
, , . :
- , , .
- , - , .
handler , handler.length.
, , ( ), :
function ask(question, ...handlers) {
let isYes = confirm(question);
for(let handler of handlers) {
if (handler.length == 0) {
if (isYes) handler();
} else {
handler(isYes);
}
}
}
// ,
// ,
ask("?", () => alert(' '), result => alert(result));
.
counter :
function sayHi() {
alert("");
// ,
sayHi.counter++;
}
sayHi.counter = 0; //
sayHi(); //
sayHi(); //
alert( ` ${sayHi.counter} ` ); // 2
, , sayhi.counter = 0 counter . , counter let counter .
, , . . .
. , , :
function makeCounter() {
// :
// let count = 0
function counter() {
return counter.count++;
};
counter.count = 0;
return counter;
}
let counter = makeCounter();
alert( counter() ); // 0
alert( counter() ); // 1
count , .
, ?
, count , . . , :
function makeCounter() {
function counter() {
return counter.count++;
};
counter.count = 0;
return counter;
}
let counter = makeCounter();
counter.count = 10;
alert( counter() ); // 10
, .
Named Function Expression
Named Function Expression, NFE Function Expressions, .
, Function Expression:
let sayHi = function(who) {
alert(`, ${who}`);
};
:
let sayHi = function func(who) {
alert(`, ${who}`);
};
? "func"?
, Function Expression. "func" function Functional Declaration, .
.
sayHi():
let sayHi = function func(who) {
alert(`, ${who}`);
};
sayHi(""); // ,
func, :
- .
- .
, sayHi "" who :
let sayHi = function func(who) {
if (who) {
alert(`, ${who}`);
} else {
func(""); // func
}
};
sayHi(); // ,
// :
func(); // , func ( )
func? , sayHi ?
:
let sayHi = function(who) {
if (who) {
alert(`, ${who}`);
} else {
sayHi("");
}
};
, sayHi . , :
let sayHi = function(who) {
if (who) {
alert(`Hello, ${who}`);
} else {
sayHi("Guest"); // : sayHi
}
};
let welcome = sayHi;
sayHi = null;
welcome(); // , sayHi !
, sayHi . sayHi, . sayHi null.
, Function Expression, .
, :
let sayHi = function func(who) {
if (who) {
alert(`, ${who}`);
} else {
func(""); //
}
};
let welcome = sayHi;
sayHi = null;
welcome(); // , ( )
, "func" . ( ). , .
sayHi welcome. func , .
, , Function Expression, Function Declaration. Function Declaration .
, , Function Declaration Named Function Expression.
.
:
name. , , JavaScript (, ).length. , rest , .
Function Expression ( ), , Named Function Expression. , ,
. JavaScript .
. , jQuery , $. lodash _, _.clone, _.keyBy (. , ). , , , . .
, , .
<code>,<pre>, 10 (plnkr, jsbin, codepen)