---
title: "面向对象"
author: "djob2008"
date: "2009-12-11T12:59:06Z"
tags: ["JavaScript"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2009-12-11T12:59:06Z"
id: 546332
url: "https://lovely.iteye.com/blog/546332"
---

#### 1/Creating objects using new Object()

```
<script language="javascript" type="text/javascript">
<!--

person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

person.run = function() {
	this.state = "running"
	this.speed = "4ms^-1"
}

//-->
</script>
```

 

#### 2/Creating objects using Literal Notation

```
<script language="javascript" type="text/javascript">
<!--

// Object Literals

timObject = {
	property1 : "Hello",
	property2 : "MmmMMm",
	property3 : ["mmm", 2, 3, 6, "kkk"],
	method1 : function(){alert("Method had been called" + this.property1)}
};

timObject.method1();
alert(timObject.property3[2]) // will yield 3

var circle = { x : 0, y : 0, radius: 2 } // another example

// nesting is no problem.
var rectangle = { 
	upperLeft : { x : 2, y : 2 },
	lowerRight : { x : 4, y : 4}
}

alert(rectangle.upperLeft.x) // will yield 2

//-->
</script>
```