http://www.jb51.net/article/7955.htm

函数是JavaScript中很重要的一个语言元素,并且提供了一个function关键字和内置对象Function,下面是其可能的用法和它们之间的关系。

使用方法一:

var foo01 = function() //or fun01 = function()  
 {  
     var temp = 100;  
     this.temp = 200;  
     return temp + this.temp;  
 }

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;  
 }

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一个最大的特性:灵活!能省就省。

关于函数初始化优先级这个问题,可以参看:"JS类定义原型方法的两种实现的区别"的回复。

function, new function, new Function之间的区别相关推荐

  1. js中(function(){}()),(function(){})(),$(function(){});之间的区别

    1. (function(){}())与(function(){})() 这两种写法,都是一种立即执行函数的写法,即IIFE (Immediately Invoked Function Express ...

  2. $(window).load(function() {})和$(document).ready(function(){})的区别

    在我以前的开发中,一般用到javascript,我都是采用jquery的模式,也就是大多数时候,第一行写的是: $(document).ready(function(){ ... }); 这个时候,不 ...

  3. JQuery $(function(){})和$(document).ready(function(){})

    document.ready和onload的区别--JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...

  4. oracle存储过程function,oracle 存储过程跟function

    当前位置:我的异常网» 数据库 » oracle 存储过程跟function oracle 存储过程跟function www.myexceptions.net  网友分享于:2014-11-26   ...

  5. 每日一题(二三)function Foo(){ Foo.a = function(){console.log(1); } this.a = function(){console.log(2)}) Fo

    题目描述:写出下面代码打印结果,并解释为什么 function Foo(){Foo.a = function(){console.log(1);}this.a = function(){console ...

  6. function is neither a registered temporary function nor a permanent function registered in the datab

    Exception in thread "main" org.apache.spark.sql.AnalysisException: Undefined function: 'gr ...

  7. bar.bind.bind_JavaScript中的function.prototype.bind和function.prototype.length解释

    bar.bind.bind 功能绑定 (Function Bind) bind is a method on the prototype of all functions in JavaScript. ...

  8. ValueError: Creating variables on a non-first call to a function decorated with tf.function.解决方法

    参考Creating variables on a non-first call to a function decorated with tf.function.解决方法!_时时勤拂拭.的博客-CS ...

  9. Creating variables on a non-first call to a function decorated with tf.function.解决方法!

    Creating variables on a non-first call to a function decorated with tf.function.解决方法! Creating varia ...

最新文章

  1. 数字大脑学术沙龙:“城市大脑与应急管理专题研讨会”成功召开
  2. Android中Google Drive显示黑屏问题分析
  3. JVM性能调优监控工具总结
  4. java 同步中的线程出现异常会放弃锁吗
  5. LightOJ - 1237 Cyber Cafe(费用流)
  6. linux和windows的进程的虚拟地址空间
  7. 缓存淘汰策略—LRU算法(java代码实现)
  8. Java基础学习记录
  9. a1在c语言里代指什么意思,A1考试宝典
  10. import java文件,java 文件夹操作,java文件夹,import java.
  11. Dropbox被墙了,幸亏还有类似服务SugarSync
  12. makefile中 = ,:=,+=有怎么区别?
  13. WebService高级,可靠消息
  14. Java学习视频教程 云析学院Java高级架构实战系列
  15. oracle11g64位怎么用sql,Oracle11g64位下使用PLSQL
  16. 51单片机贪吃蛇程序
  17. FastReport 2022-2 VCL 企业版-FastReport
  18. python sha1加密字符串
  19. 莱维特LEWITT声卡驱动安装设置方法
  20. 在自己电脑上无法用Foxmail客户端登录126邮箱的解决办法

热门文章

  1. 全程软件测试之测试需求分析与计划 1
  2. IE9 IE10 无法上传文件 解决方案
  3. NOAA官网下载数据
  4. IE浏览器按Backspace返回上一页面
  5. SOLIDWORKS官网如何下载SOLIDWORKS安装程序?
  6. 多线程编程(下):线程同步通信
  7. 如何用python做游戏(简单易上手版)【送 源码】
  8. 《谷歌眼镜》新书作者:眼镜需要成为AR的载体吗?
  9. 无盘服务器0x00000124,windows 7 蓝屏代码0X00000124
  10. DWG转PDF文件(Any DWG to PDF Converter) 2014 汉化破解版