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