Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created July 2, 2014 04:25
Show Gist options
  • Save hjzheng/f937de1fd8646ce62cc3 to your computer and use it in GitHub Desktop.
Save hjzheng/f937de1fd8646ce62cc3 to your computer and use it in GitHub Desktop.
使用new 操作符function里面到底发生了什么?
当我们在使用new操作符,函数(构造函数)中到底放生了什么?
1.创建一个空对象
2.让空对象的__proto__(这个不同浏览器不同)成员指向了构造函数对象prototype成员对象
3.将构造函数对象的this指针替换成空对象,然后再调用Base函数
4.返回this对象
例如
function Foo(name){
this.name = name;
}
new Foo("bar");
1.var obj = {};
2.obj.__proto__ = Foo.prototype;
3.Foo.call(obj);
... ...
4.return this;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment