函数是JavaScript中很重要的一个语言元素,提供了一个function关键字和内置对象Function,下面是其可能的用法和它们之间的关系。
使用方法一:
var foo01 = function() //or fun01 = function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
等价于:
var 匿名函数 = function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
};
foo01 = 匿名函数();
alert(typeof(foo01));
alert(foo01());
运行结果:
function
300
最普通的function使用方式,定一个JavaScript函数。两种写法表现出来的运行效果完全相同,唯一的却别是后一种写法有较高的初始化优先级。在大扩号内的变量作用域中,this指代foo01的所有者,即window对象。
使用方法二:
var foo02 = new function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
等价于:
function 匿名类()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
var foo02 = new 匿名类();
alert(typeof(foo02));
alert(foo02.constructor());
运行结果:
object
300
这是一个比较puzzle的function的使用方式,好像是定一个函数。但是实际上这是定一个JavaScript中的用户自定义对象,不过这里是个匿名类。这个用法和函数本身的使用基本没有任何关系,在大扩号中会构建一个变量作用域,this指代这个作用域本身。
使用方法三:
var foo3 = new Function(’var temp = 100; this.temp = 200; return temp + this.temp;’);
alert(typeof(foo3));
alert(foo3());
运行结果:
function
300
使用系统内置函数对象来构建一个函数,这和方法一中的第一种方式在效果和初始化优先级上都完全相同,就是函数体以字符串形式给出。
使用方法四:
var foo4 = Function(’var temp = 100; this.temp = 200; return temp + this.temp;’);
alert(typeof(foo4));
alert(foo4());
运行结果:
function
300
这个方式是不常使用的,效果和方法三一样,不过不清楚不用new来生成有没有什么副作用,这也体现了JavaScript一个最大的特性:灵活!能省就省。
分享到:
相关推荐
function, new function, new Function之间的区别
smoothing a function
”你可能并不知道可以传给 new Function() 一个字符串作为函数的body来构造一个 JavaScript函数。编程中并不经常用到,但有时候应该是很有用的. 下面是 new Function 的基本用法: 代码如下:// 最后一个参数是函数的 ...
情景一:var yx01 = new function() {return “圆心”}; alert(yx01); 我们运行情景一代码,将返回显示“[object object] ”,此时该代码等价于: function 匿名类(){ return “圆心”; } var yx...
New insights into function of the growth plate
ofdm matlab energy new
本文实例讲述了ES6中new Function()语法及应用。分享给大家供大家参考,具体如下: 学习《ECMAScript6入门》中的模板字符串的案例中看见了new Function()创建函数的语法: let str = 'return ' + '`Hello ${name}!`...
Namespace xFunction Public Class f Const NumList = "零壹贰叁肆伍陆柒捌玖" Const UnitList = "仟佰拾亿仟佰拾万仟佰拾元角分" Public Shared Sub StringList() Dim tmepList As New List(Of String) ...
恭喜你答对了,其实啊,函数的定义常见的也就是上面的这种方式,没有什么特别的花样,但是今天咱们聊聊new Function这个新朋友,其实算是大佬,叫做:运筹于帷幄之中,决胜千里之外,看看它是怎么创建函数的: ...
FTrace is a relatively new kernel tool for tracing function execution in the Linux kernel. Recently, FTrace added the ability to trace function exit in addition to function entry. This allows for ...
本篇文章小编将为大家介绍,关于eval 与new Function 到底该选哪个?有需要的朋友可以参考一下
js中function对象是一个...return new Function(" return scope"); } constructFunction()(); function constructFunction2(){ var scope="local"; return function(){ return scope; } } constructFunction2()();
函數指標相關範例,1.函數指標 2.函數指標當函數的引數 3.函數指標當函數的回傳值類別內的 1.2.3怎麼寫 怎麼透過函數指標呼叫函數
些代码可以干什么?可以肯定的是可以干很多猥琐的事
主要介绍了JS对象与JSON互转换、New Function()、 forEach()、DOM事件流等js开发中基础的知识点,并通过举例详细解释了JavaScript定义的数据类型、无第三变量交换值、/和%运算符、Memoization技术、闭包等知识,需要...
function newtxtfile(filename,text:shortstring):boolean;stdcall;External 'reg.dll' function appendtxtfile(filename,text:shortstring):boolean;stdcall;External 'reg.dll' function readtxtfile(filename:...
(function(a){a.N={VERSION:...return function(c){return c[b]=c[b]||"_newmap_id_"+ ++a,c[b]}}(),requestAnimFrame:function(){function a(a){window.setTimeout(a,1e3/60)}var b=window.requestAnimationFrame||