# Creating object methods

**URL:** <https://forum.kirupa.com/t/creating-object-methods/5093>\
**Category:** Uncategorized\
**Created:** [August 31, 2002, 12:38pm UTC](https://forum.kirupa.com/t/creating-object-methods/5093 "2002-08-31T12:38:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pruijter](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pruijter](https://forum.kirupa.com/u/pruijter)\
**Post date:** [August 31, 2002, 12:38pm UTC](https://forum.kirupa.com/t/creating-object-methods/5093/1 "2002-08-31T12:38:22Z")

</div>

Hello,

How do I create a method in an object,  
Thanks  
Pierre

example:  
// code  
function house(type, location) // constructor “house”  
{  
this.type = type;  
this.location = location;  
}

house1 = new house(villa, hawai); //creating an instance  
// end code

For retrieving e.g. the variables type and location, how do I create the methods getType and getLocation.

I expected the following would work but it does not:  
//code  
function house(type, location) //constructor  
{  
this.type = type;  
this.location = location;

```
    function getType() // create method getType
    { return type; }
    function getLocation() // create method getLocation
    { return location; }

```

}

house1 = new house(villa, hawai); //create an instance  
houseType1 = house1.getType(); // retrieve variables  
houseLocation1 = house1.getLocation();  
trace( houseType+" "+houseLocation1); // print variables

// end code

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2002, 8:35am UTC](https://forum.kirupa.com/t/creating-object-methods/5093/2 "2002-09-01T08:35:48Z")

</div>

i see what you are trying to do but i dont know how to achieve it 🙂 oh well maybe someone else might?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2002, 1:57pm UTC](https://forum.kirupa.com/t/creating-object-methods/5093/3 "2002-09-01T13:57:38Z")

</div>

Several ways to do that. The easiest would be to add a method to the prototype of your class (which should only contain the methods of the Object class for now):

```auto
house.prototype.getType=function ()
{ 
    return this.type; 
}

```

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2002, 2:04pm UTC](https://forum.kirupa.com/t/creating-object-methods/5093/4 "2002-09-01T14:04:04Z")

</div>

You make quite a big mistake by the way, forgetting **this** in your function. You **have to put it** whenever you create a method.

pom 😛
