new操作符做了什么??
在javascript中,new操作符随处可见,我讲一下我自己对new操作符的理解。。。
构造函数无返回值
//测试代码
function Foo(name) {
var age = 20;
this.name = name;
} Foo.prototype.getName = function() {
return this.name;
} var foo = new Foo("Jack"); console.log(foo.age); //undefined
console.log(foo.name); //Jack
console.log(foo.getName()); //Jack
new的具体过程(借用大神的代码):
function internalConstructor(parameters ) {
O = new Object(); //new一个空对象
O.[[Class]] = "Object"; //类型是Object
//如果F.prototype是Object,O.[[Ptototype]](也就是O.__proto__)为F.prototype,否则为Object.prototype
O.[[Prototype]] = Type(F. prototype) === 'Object' ?
F.prototype : Object.prototype;
//在对象O的作用域下,执行F.[[call]] 函数,能够复制构造函数的this属性到实例对象
R = F.[[Call]].apply(O, parameters);
//如果R是Object,则返回R,否则返回O,也就是说构造函数存在this相关属性,则返回R,无this相关属性,直接返回O
return Type(R) === 'Object' ? R : O;
}
构造函数存在返回值
1、如果返回(return)一个原始类型(无 return 时其实为return原始类型undefined),那么就返回new创建的匿名对象
//测试代码
function Foo(name) {
var age = 20;
this.name = name;
return 1;
} Foo.prototype.getName = function() {
return this.name;
} var foo = new Foo("Jack"); console.log(foo.age); //undefined
console.log(foo.name); //Jack
console.log(foo.getName()); //Jack
2、只要new表达式之后的构造函数返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象
//测试代码
function Foo(name) {
var age = 20;
this.name = name;
return {};
} Foo.prototype.getName = function() {
return this.name;
} var foo = new Foo("Jack"); console.log(foo); //Object {}
console.log(foo.age); //undefined
console.log(foo.name); //undefined
console.log(foo.getName()); //Uncaught TypeError: foo.getName is not a function
正常的构造函数是没有返回值的,如果有返回值的话,直接执行构造函数获得返回值就行,也就没必要new了。。。。。。。
new操作符做了什么??的更多相关文章
- new 操作符 做了什么
new 操作符 做了什么 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例. 假设Test是一个构造函数,通常在创建对象的实例时,要使用new,eg:test = new ...
- JavaScript下的new操作符做了什么?
可以参考知乎的一篇文章:https://zhuanlan.zhihu.com/p/23987456 参考网上其他人的文章,new发生了以下操作 参考MDN:https://developer.mozi ...
- C#的new操作符到底做了什么
使用new操作符来创建对象,其背后到底发生了什么? 有一个父类Animal,Dog派生于Animal. class Program { static void Main(string[] args) ...
- C# new关键字和对象类型转换(双括号、is操作符、as操作符)
一.new关键字 CLR要求所有的对象都通过new来创建,代码如下: Object obj=new Object(); 以下是new操作符做的事情 1.计算类型及其所有基类型(一直到System.Ob ...
- C++ 中 new 操作符内幕:new operator、operator new、placement new
一.new 操作符(new operator) 人们有时好像喜欢有益使C++语言的术语难以理解.比方说new操作符(new operator)和operator new的差别. 当你写这种代码: st ...
- [c++基础]3/5原则--拷贝构造函数+拷贝赋值操作符
/* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> #include &qu ...
- LiveScript 操作符
The LiveScript Book The LiveScript Book 操作符 数字 标准的数学操作符: 1.1 + 2 # => 32.3 - 4 # => -13.6 ...
- 实现一个new操作符
new 操作符做了这些事: 1.它创建了一个全新的对象: 2.它会被执行[[Prototype]](也就是__proto__)链接: 3.它使this指向新创建的对象: 4.通过new创建的每个对象最 ...
- js中的new操作符解析
new 操作符做了以下事情: 1.创建一个对象,将对象赋值给this function Person(name, age) { console.log(this) //Person {} } let ...
随机推荐
- autoLayout 纯代码
SB中拖好空间,让后分别在,Pin,Align,Resolve Auto Layout Issues三个面板中设置好约束就好了. 用存代码的方式给控件添加约束,完成自动布局: 利用NSLayoutCo ...
- Using Vertex Texture Displacement for Realistic Water Rendering
http://blog.csdn.net/soilwork/article/details/709869 Using Vertex Texture Displacement for Realistic ...
- UE4.7的IOS发布和调试的相关问题
UE4.7以后正式源码免费了,正好最近工作也在做这部分,ue4的官方文档虽然有一部分ios平台的资料,那也只是通过编辑器来发布或预览一类,但手游程序员都知道,一些cpu和gpu性能上的调试是在所难免的 ...
- Heterogeneous System Architecture
https://en.wikipedia.org/wiki/Heterogeneous_System_Architecture Steps performed when offloading calc ...
- Java单链表的实现
将结点Node进行封装,假设Node的操作有增加,删除,查找,打印几个操作.将Node实现为链表Link的内部类,简化代码. package Chapter5; import java.securit ...
- 关于C和C++动态链接库的几个问题
问题: 1.写一段C++程序,编译成动态链接库后,C程序怎么访问? 2.写一段C程序,编译成动态链接库后,C++程序怎么访问? 3.写一个类,编译成动态链接库后,里面的public变量能否访问? 对于 ...
- Delphi 指针
1:指针的赋值. type RTestInfo = record Age:Integer; end; PtestInfo = ^ RtestInfo; var Test1,Test2:PtestInf ...
- nginx下php频繁卡死502
解决:[WARNING] fpm_children_bury(), line 215: child 2736 (pool default) exited on signal 15 SIGTERM af ...
- sqlserver 计算 百分比
,),))+'%' As 百分比 NUMERIC(P,S) P的默认值是:38 S的默认值是:-84~127 numeric(a,b)函数有两个参数,前面一个为总的位数,后面一个参数是小数点后的位数, ...
- key_value 类型配置文件的解析
#include <iostream> #include <string> #include <stdint.h> #include <map> #in ...