今天看别人的博客,好多都提出了顶级函数这个词.这个词到底是什么意思?不理解 这个是原文:用Function()构造函数创建一个函数时并不遵循典型的作用域,它一直把它当作是顶级函数来执行。var y = "global"; function constructFunction() { var y = "local"; return new Function("return y"); // 无法获取局部变量}alert(constructFunction()()); // 输出 "global"
没听过这种说法呢。估计说的是全局函数。
(function (){ var foo=10; var bar=2; alert(foo*bar); })() 这个函数定义后会自动被执行,你说的是这个意思么
说的是高阶函数吧
/** * 高阶函数 - 操作函数的函数,可以把一个或者多个函数作为参数,并返回一个新的函数; */ function not(f){ return function(){ // 这里 return 的是函数哦 /*var result = !f.apply(this,arguments); if(!result){ console.log(arguments[0]); } return result;*/ return !f.apply(this,arguments); } } /* 数组 every 方法的回调函数,这个方法有三个参数: * value(当前元素的值)、 * index(当前元素的索引)、 * array(数组实例)。 */ function even(value, index, ar) { /*var result = value % 2 === 0; if(!result){ console.log(arguments[0]); } return result;*/ return value % 2 === 0; } var arr = [2, 5]; /** * every 方法会按升序顺序对每个数组元素调用一次传入 callback 函数,直到 callback 函数返回 false; * 如果找到导致 callback 返回 false 的元素,则 every 方法会立即返回 false。 否则,every 方法返回 true; * 如果 callback 参数不是函数对象,则将引发 TypeError 异常; * thisArg 可选。可在 callback 函数中为其引用 this 关键字的对象。 * 如果省略 thisArg,则 undefined 将用作 this 值。 * eg. array1.every(callback[, thisArg]) */ if (arr.every(even)) { console.log("All are even."); } else { console.log("Some are not even."); } if (arr.every(not(even))) { console.log("All are odd."); } else { console.log("Some are not odd."); }
顶级函数???what?有这个说法?
估计指的是window.xxx = function(){} 这种
window.xxx = function(){}
作用域链层级的顶层 ? 大概是这意思?
意思就是说在JS块中被直接调用
说法不太严谨,应该是顶层函数,没有“顶级函数”的说法,应该是查询作用域链的时候处于最顶层的函数吧
没听过这种说法呢。估计说的是全局函数。
(function (){
var foo=10;
var bar=2;
alert(foo*bar);
})()
这个函数定义后会自动被执行,你说的是这个意思么
说的是高阶函数吧
顶级函数???what?有这个说法?
估计指的是
window.xxx = function(){}
这种作用域链层级的顶层 ? 大概是这意思?
意思就是说在JS块中被直接调用
说法不太严谨,应该是顶层函数,没有“顶级函数”的说法,应该是查询作用域链的时候处于最顶层的函数吧