---
title: "js new this"
author: "caiceclb"
date: "2010-04-07T14:33:18Z"
tags: []
categories: ["Web前端"]
slug: "js-new-this"
draft: false
lastmod: "2010-04-07T14:34:24Z"
id: 636259
url: "https://caiceclb.iteye.com/blog/636259"
---

"这篇措辞可能有误，有待深入后再论证"

 

js中的this蛮折磨人，话说this根据context（上下文）有不同。

 

发现new “诞生”了新的上下文。

 

承接上个文章所发现的一点，判断window对象 用 obj.setInterval

 

 

```
	function aa() {
		this.name = "haha";
		alert(this.setInterval);
	};
	aa();
	alert(new aa().name);
```

 

aa()弹出一个alert，new aa()还弹出一个alert，最后alert一个name，总共3次

结果依次是： function setInterval() {\[native code]}, undefined, haha

 

aa()运行上下文是window，this指向window，之后new了一个aa，上下文就变了，alert(this.setInterval)就弹出一个undefined, 然后name正确被弹出，这说明this指向的是new出的新对象。

 

或许明白了一些东西在里面。