以下来自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

new constructor[([arguments])]

Parameters

constructor
A function that specifies the type of the object instance.
arguments
A list of values that the constructor will be called with.

Description

Creating a user-defined object requires two steps:

  1. Define the object type by writing a function.
  2. Create an instance of the object with new.

To define an object type, create a function for the object type that specifies its name and properties. An object can have a property that is itself another object. See the examples below.

When the code new foo(...) is executed, the following things happen:

  1. A new object is created, inheriting from foo.prototype.
  2. The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments.
  3. The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)

You can always add a property to a previously defined object. For example, the statement car1.color = "black" adds a property color to car1, and assigns it a value of "black". However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of the Carobject type.

You can add a shared property to a previously defined object type by using the Function.prototype property. This defines a property that is shared by all objects created with that function, rather than by just one instance of the object type. The following code adds a color property with value null to all objects of type car, and then overwrites that value with the string "black" only in the instance object car1. For more information, see prototype.

 function Car() {}
car1 = new Car() alert(car1.color) // undefined Car.prototype.color = null
alert(car1.color) // null car1.color = "black"
alert(car1.color) // black

Example: Object type and object instance

Suppose you want to create an object type for cars. You want this type of object to be called car, and you want it to have properties for make, model, and year. To do this, you would write the following function:

function car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
Now you can create an object called mycar as follows: var mycar = new car("Eagle", "Talon TSi", 1993);
This statement creates mycar and assigns it the specified values for its properties. Then the value of mycar.make is the string "Eagle", mycar.year is the integer 1993, and so on. You can create any number of car objects by calls to new. For example:
var kenscar = new car("Nissan", "300ZX", 1992);

Example: Object property that is itself another object

Suppose you define an object called person as follows:

function person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}

And then instantiate two new person objects as follows:

var rand = new person("Rand McNally", 33, "M");
var ken = new person("Ken Jones", 39, "M");

Then you can rewrite the definition of car to include an owner property that takes a person object, as follows:

function car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}

To instantiate the new objects, you then use the following:

var car1 = new car("Eagle", "Talon TSi", 1993, rand);
var car2 = new car("Nissan", "300ZX", 1992, ken);

Instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects rand and ken as the parameters for the owners. To find out the name of the owner of car2, you can access the following property:

car2.owner

详解new function(){}和function(){}() 区别分析

只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实为 return 原始类型 undefined),那么就返回 new 创建的匿名对象

情景一:
var yx01 = new function() {return "圆心"}; 
alert(yx01); 
我们运行情景一代码,将返回显示“[object object] ”,此时该代码等价于:

function 匿名类(){ 
    return "圆心"; 

var yx01 = new 匿名类(); 
alert(yx01);我们对情景一的代码进行下面改造:

var yx01 = new function() {return new String("圆心")}; 
alert(yx01); 
我们运行,将会发现返回的是“圆心”,这是为什么呢?

只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实为 return 原始类型 undefined),那么就返回 new 创建的匿名对象
由于 new String 会构造一个对象,而不是一个 string 直接量,且new String(x) 如果带参数,那么alert它的时候就会返回 x。所以 yx01 将返回 new String(”圆心”) 这个对象,而 alert yx01 则显示 “圆心”。

情景二:

var yx02 = function() {return "圆心"}(); 
alert(yx02);我们运行情景二代码,将返回显示“圆心”,此时该代码等价于:

var 匿名函数 = function() {return "圆心"}; 
yx02 = 匿名函数(); 
alert(yx02);很明显,yx02 返回的是匿名函数的执行结果值,即 yx02 为:“圆心”。

当然匿名函数的执行结果也可以为一个匿名对象。具体常见应用可以看《Javascript的一种模块模式

具体有

var func=function(a,b)
{
this.a=a;
this.b=b;
}
var funcO=new func(1,2);
Function.prototype.say=function(){ alert('jack');};

document.write(func.say()); //可以
document.write(funcO.say());//报错

在上面例子中。func是object type对象,funcO是对象实例object instance。

凡是Function类型的对象都可以调用Object的prototype,但返过来就不行。

上面的最后一句报错funcO.say() funcO没有say方法。就说明了这个问题。

obejct type的constructor都返回Function类型。但是object instance返回的是object type。

document.write(func.constructor==Function); //true
document.write(funcO.constructor==func); //true

根据以上所说,可以明白:Object,Function,Number,String,Boolean,undefined,后面一个就不说了。前面5个typeof一下都是'function'串。例:alert( typeof Object == 'function' );和alert( Object );就明了了。(这也就是为什么XXX.constructor可以直接和她们画等号的原因。)

---------------------------

一个例子

<script type="text/javascript">
var name = "Kevin Yang";
function sayHi(){
alert("你好,我的名字叫" + this.name);
}
function Person(name){
this.name = name;
}
Person.prototype.sayHello = sayHi;
var marry = new Person("Marry");
marry.sayHello();
var kevin = new Person("Kevin");
kevin.sayHello();
script>

在上面这段代码中,我们定义了一个Person的“类”(实际上还是一个对象),然后在这个类的原型(类原型相当于C++中的静态成员变量的概念)中定义了sayHello属性,使其指向全局的sayHi对象。运行代码我们可以看到,marry和kevin都成功的向我们打了声“招呼”。

在这段代码中有两点需要思考的,一个是new我们很熟悉,但是在这里new到底做了什么操作呢?另外一个是,这里执行sayHello的时候,this指针为什么能够正确的指向marry和kevin对象呢

我们来把上面定义“类”和实例化类对象的操作重新“翻译”一下:

<script type="text/javascript">
var name = "Kevin Yang";
function sayHi(){
alert("你好,我的名字叫" + this.name);
}
function Person(name){
var this;
this.name = name;
return this;
}
Person.prototype.sayHello = sayHi;
var marry = Person("Marry");
marry.sayHello();
var kevin = Person("Kevin");
kevin.sayHello();
script>

当然这段代码并不能正确执行,但是它可以帮助你更好的理解这个过程。

当我们使用new关键字实例化一个“类”对象的时候,Javascript引擎会在这个对象内部定义一个新的对象并将其存入this指针。所有此对象内部用到this的代码实际上都是指向这个新的对象。如this.name = name,实际上是将参数中的name对象赋值给了这个新创建的对象。函数对象执行完之后Javascript引擎会将此对象返回给你,于是就有 marry变量得到的对象的name为“Marry”,而kevin变量得到的对象的name属性确实“Kevin”。

js Function 加不加new 详解的更多相关文章

  1. ext.js的mvc开发模式详解

    ext.js的mvc开发模式详解和环境配置 在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs 4.x版本中引入了MVC开 ...

  2. 微信JS接口汇总及使用详解

    这篇文章主要介绍了微信JS接口汇总及使用详解,十分的全面.详尽,包含分享到朋友圈,分享给朋友,分享到QQ,拍照或从手机相册中选图,识别音频并返回识别结果,使用微信内置地图查看位置等接口,有需要的小伙伴 ...

  3. js对象浅拷贝和深拷贝详解

    js对象浅拷贝和深拷贝详解 作者:i10630226 字体:[增加 减小] 类型:转载 时间:2016-09-05我要评论 这篇文章主要为大家详细介绍了JavaScript对象的浅拷贝和深拷贝代码,具 ...

  4. JS中的event 对象详解

    JS中的event 对象详解   JS的event对象 Event属性和方法:1. type:事件的类型,如onlick中的click:2. srcElement/target:事件源,就是发生事件的 ...

  5. js中鼠标滚轮事件详解

    js中鼠标滚轮事件详解   (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ ) 之前js 仿Photoshop鼠标滚轮控制输入框取值中已使用 ...

  6. js中中括号,大括号使用详解

    js中中括号,大括号使用详解 一.总结 一句话总结:{ } 是一个对象,[ ] 是一个数组 1.js大括号{}表示什么意思? 对象 { } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或 ...

  7. amazeui中的js插件有哪些(详解功能)

    amazeui中的js插件有哪些(详解功能) 一.总结 一句话总结: 二.amazeui中的js插件有哪些 1.UI 增强 警告框Alert 按钮交互Button 折叠面板Collapse 下拉组件D ...

  8. C编译器、链接器、加载器详解

    摘自http://blog.csdn.net/zzxian/article/details/16820035 C编译器.链接器.加载器详解 一.概述 C语言的编译链接过程要把我们编写的一个c程序(源代 ...

  9. JS函数动作分层结构详解及Document.getElementById 释义 js及cs数据类型区别 事件 函数 变量 script标签 var function

    html +css 静态页面 js     动态 交互   原理: js就是修改样式, 比如弹出一个对话框. 弹出的过程就是这个框由disable 变成display:enable. 又或者当鼠标指向 ...

随机推荐

  1. discuz默认模板文件结构详解-模板文件夹介绍

    | — template — default   系统内置风格模板(默认风格)| — template — default  – discuz_style_default.xml  风格安装文件,可用 ...

  2. 关于js的一些关键知识点(call,apply,callee, caller,clourse,prototypeChain)

    可能不少学习javascript在使用call,apply,callee时会感到困惑,以下希望对于你有所帮助: 1.~~~call ,apply是函数(函数对象)的方法:callee是函数argume ...

  3. QT Sleep(最佳的平衡:一边发送消息,一边睡眠)

    转自:http://xiangjie88.iteye.com/blog/898417 sleep()//秒msleep()//毫秒usleep()//微秒以前为了模拟鼠标点击用过这些函数,可以让进程中 ...

  4. Delphi程序自删除的几种方法

    program Project1; uses SysUtils, windows; var f:textfile; a:string; begin a:=paramstr(); assignfile( ...

  5. ImageMagick还是GraphicsMagick?

    引自:http://co63oc.blog.51cto.com/904636/328997 ImageMagick(IM) 套装包含的命令行图形工具是一主要自由软件:Linux,其他类Unix操作系统 ...

  6. HDU 3571 N-dimensional Sphere

    高斯消元,今天数学死了无数次…… #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  7. Oracle 快照及 dblink使用 (两台服务器数据同步)

    /*一.创建dblink:*/ --1.在目的数据库上,创建dblin drop database link dblink_anson; Create public database link dbl ...

  8. 微软正式公布Visual Studio 2013 Update 3 (2013.3) RTM

     昨天微软的Visual Studio 2013 Update 3(Visual Studio 2013.3)正式公布(RTM)了,做为微软认证金牌合作的葡萄城控件,我们组织力量第一时间进行翻译. ...

  9. 使用AJAX日历控件,显示某些日期(CalendarExtender)

    1. The HTML Markup <div> <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1&q ...

  10. plsql developer连接64位Oracle11g的解决方法

    1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...