Function
- The Name property of the function returns the names of the functions;
toStringthe use of a multi-line string can be implemented in disguise:
var multiline = function (fn) { var arr = fn.toString().split(‘\n’); return arr.slice(1, arr.length - 1).join(‘\n’);};function f() {/* 这是一个 多行注释*/}multiline(f);// “ 这是一个// 多行注释”
- Note that for the var command, local variables can only be declared inside the function, declared in other chunks, and all are global variables;
- The scope at which the function executes is the scope at which it was defined, not the scope at which it was called:
var a = 1;var x = function () { console.log(a);};function f() { var a = 2; x();}f() // 1//很容易犯错的一点是,如果函数A调用函数B,却没考虑到函数B不会引用函数A的内部变量。var B = function () { console.log(a);};function A(f) { var a = 2; f();}A(B)// ReferenceError: a is not defined
- The runtime does not give an error, no matter how many parameters are supplied (or if no parameters are supplied). The value of the omitted parameter becomes undefined;
- The length property of a function is independent of the number of arguments actually passed in, reflecting only the number of arguments the function expects to pass in.
- Return cannot be used alone and must be used in a function;
- The most common scenario for Eval is to parse the JSON data string, but the correct approach should be to use the native Json.parse method;
Strict mode andevalWith
- The external scope code cannot get
eval the variables defined inside:
(function f() { ‘use strict’; eval(‘var foo = 123’); console.log(foo); // ReferenceError: foo is not defined})()
- But
eval the values of variables that can read and write to the external scope:
(function f() { ‘use strict’; var foo = 1; eval(‘foo = 2’); console.log(foo); // 2})()
- In order to ensure that
eval aliases do not affect code optimization, JAVASCRIPT standards stipulate that all aliases eval are performed with eval an internal global scope:
//下面这些形式都是eval的别名调用,作用域都是全局作用域。eval.call(null, ‘…’)window.eval(‘…’)(1, eval)(‘…’)(eval, eval)(‘…’)
Java Basic Learning experience