function, new function, new Function之间的区别
http://www.jb51.net/article/7955.htm
使用方法一:
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之间的区别相关推荐
- js中(function(){}()),(function(){})(),$(function(){});之间的区别
1. (function(){}())与(function(){})() 这两种写法,都是一种立即执行函数的写法,即IIFE (Immediately Invoked Function Express ...
- $(window).load(function() {})和$(document).ready(function(){})的区别
在我以前的开发中,一般用到javascript,我都是采用jquery的模式,也就是大多数时候,第一行写的是: $(document).ready(function(){ ... }); 这个时候,不 ...
- JQuery $(function(){})和$(document).ready(function(){})
document.ready和onload的区别--JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...
- oracle存储过程function,oracle 存储过程跟function
当前位置:我的异常网» 数据库 » oracle 存储过程跟function oracle 存储过程跟function www.myexceptions.net 网友分享于:2014-11-26 ...
- 每日一题(二三)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 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
最新文章
- 模型大十倍,性能提升几倍?谷歌研究员进行了一番研究
- Python处理时间日期模块
- Python 连接FTP服务器并实现文件夹下载实例演示,python区分ftp目录下文件和文件夹方法,ftp目录下包含中文名问题处理
- 九大技巧教你快速提升移动应用登陆转化率
- ThreadLocal的意义和实现
- 钢材规格解读的软件_万维软件ERP五金建材行业解决方案
- C++ lower_bound 与 upper_bound 函数
- 历经十年,Windows 7 生命周期将结束,微软:快换 Windows 10
- 个人三观的东西(1)
- 重负载Telnet BBS系统优化和维护经验谈
- FLAG_ACTIVITY_CLEAR_TOP和singleTask的区别
- 【经验分享】U盘软刷映泰TB250-BTC刷魔改BIOS上6789代CPU,另解决开机转一下后停止问题
- 计算机管理里面和打印机相关的,打印机设备
- 每天有8千人在逃离北上广!扎心不?
- html vertical-align:middle垂直居中 的方法
- chkdsk /f P 很强大,帮我解决了硬盘写保护的问题
- 百练 openjudge 开餐馆(动态规划)
- 电影推荐:六部影响你恋爱观的爱情电影,勿错过
- mate20por3d人脸识别_体验华为Mate20 Pro 3D结构光 生物识别新高度
- 环境感知与规划专题(十一)——状态栅格规划器(State Lattice Planner)