---
title: "Object的copy"
author: "wangdun119"
date: "2012-04-12T15:41:55Z"
tags: []
categories: ["Web前端"]
slug: "object-copy"
draft: false
lastmod: "2012-04-12T15:41:55Z"
id: 1484545
url: "https://wangdun119.iteye.com/blog/1484545"
---

the use of an object() function that accepts an object and returns a new one that has the parent as a prototype.

```js
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
```


If you need access to an uber property, you can modify the object() function
like so:

```js
function object(o) {
var n;
function F() {}
F.prototype = o;
n = new F();
n.uber = o;
return n;
}
```

Using this function will be the same as the extendCopy(): you basically take an object such as twoDee, create a new object from it and then proceed to augmenting the new object.

```java
var triangle = object(twoDee);
triangle.name = 'Triangle';
triangle.getArea = function(){return this.side * this.height / 2;};
```

The new triangle still behaves the same way:
>>> triangle.toString()
"shape, 2D shape, Triangle"