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. Python处理时间日期模块
  3. Python 连接FTP服务器并实现文件夹下载实例演示,python区分ftp目录下文件和文件夹方法,ftp目录下包含中文名问题处理
  4. 九大技巧教你快速提升移动应用登陆转化率
  5. ThreadLocal的意义和实现
  6. 钢材规格解读的软件_万维软件ERP五金建材行业解决方案
  7. C++ lower_bound 与 upper_bound 函数
  8. 历经十年,Windows 7 生命周期将结束,微软:快换 Windows 10
  9. 个人三观的东西(1)
  10. 重负载Telnet BBS系统优化和维护经验谈
  11. FLAG_ACTIVITY_CLEAR_TOP和singleTask的区别
  12. 【经验分享】U盘软刷映泰TB250-BTC刷魔改BIOS上6789代CPU,另解决开机转一下后停止问题
  13. 计算机管理里面和打印机相关的,打印机设备
  14. 每天有8千人在逃离北上广!扎心不?
  15. html vertical-align:middle垂直居中 的方法
  16. chkdsk /f P 很强大,帮我解决了硬盘写保护的问题
  17. 百练 openjudge 开餐馆(动态规划)
  18. 电影推荐:六部影响你恋爱观的爱情电影,勿错过
  19. mate20por3d人脸识别_体验华为Mate20 Pro 3D结构光 生物识别新高度
  20. 环境感知与规划专题(十一)——状态栅格规划器(State Lattice Planner)

热门文章

  1. 跨境电商去哪做好?东南亚六国电商情况一览
  2. Vue 简单的函数式组件
  3. 强人工智能和弱人工智能,你了解多少?
  4. 【TD】CG 特效公司里 Pipeline TD 的职业生涯如何?
  5. 九问国产操作系统,九大掌门人首次同台激辩
  6. 吉他如何实现内录or通过转接头和简易声卡连接电脑没有声音怎么办
  7. Unity3d 切换场景过度动画
  8. 从Etherscan在2021年的功能更新,差不多就可以看到这一年加密世界的发展历程和未来方向
  9. 菜鳥ITer學習尤克里里【Day02】
  10. 直方图实例详解(颜色直方图、灰度直方图)