DSiPaint/WiiOperaSDK Logo
Creating/Expanding an Object - Java Script
Avatar By lilwayne1556
Posted 12 Sep 2013 05:49
Category: Technology
If you haven't read my Everything's an Object blog then please go read that one first. As I said in my last blog I would like to explain how to create your own object and expand an existing object. The reason why you would want to create an object is because you have a bunch of enemies in your game and you want to give each one their own properties but still having a global enemy property. Another reason might be because you want to make a library for your personal uses. Lets get started with an example.

var person = function(name){this.name = name;};

var wayne = new person("Wayne");
alert(wayne.name);//Alerts Wayne

This is how you define the variable person as an object.

This will create a new person object and set the name to be Wayne.

The example above shows how to create a simple object with only one property. But what if you want more than one and you don't want to set when you create the object. To do this we can use .prototype . Every object has the .prototype property within it. This allows you to add or change methods and properties for any object. Here is when things become more advanced but bare with me here. Again we will be creating the same thing as we did in example one but in a different way.

var person = function(){};
person.prototype = {
firstName:function(firstName){this.firstName = firstName;},
lastName:function(lastName){this.lastName = lastName;}

var wayne = new person();

alert(wayne.firstName + " " + wayne.lastName);//Alerts Wayne Bowie

Expands the person object.

Makes a function to set the person's first name.

Sets the first name to Wayne.

Note how we made an empty function for the person variable. The reason why is because we didn't need to run any code when we create the object. I would also like to show you a different way to expand an object.

person.prototype.firstName = function(firstName){this.firstName = firstName;};

Creates/edit the firstName method in the person object.

This method is only useful when adding/changing one method or property. Now you might be wondering the point in all this, well I'll show you why you may need to create an object.

var wayne = new person();
var bob = new person();
var sally = new person();


alert(sally.firstName);//Alerts Sally

//Alternative Method

var persons = {
personOne: {
firstName: "Wayne"
personTwo: {
firstName: "Bob"
personThree: {
firstName: "Sally"

alert(person.personOne.firstName);//Alerts Wayne

Creates multiple person objects.

Sets firstName to each person.

As you can see from above, objects will make your lives easier in the long run. Lastly is expanding an existing object such as an Array. You should only do this if you really need it and only want the method or property to be used by only one object. Here is an example

Array.prototype.maxNumber = function(){this.sort(function(a,b){return b-a;});return this[0];};

var array = [40,100,1,5,10];
alert(array.maxNumber());//Alerts 100

Expanding an core object is fine but if you edit a method or property then you should be carful. Only use this if you're an advanced Java Script user.

Hopefully this helps you out in your programs if you didn't know this already.
Featured: Yes (atreyufan1500)
+5 -2
Avatar Avatar Avatar Avatar Avatar
12 Sep 2013 15:30
C#/C++ > Java/Java Script
Avatar Luisjo96
12 Sep 2013 04:19
You and your weird japanese stuff .-.
Avatar fordcars
12 Sep 2013 04:02
Avatar NillocSkywalker
12 Sep 2013 03:34
Uuuhhhmmmm.... This is a confusing way to explain Java Script. I just did the first unit of Java Script on Codecademy.com , it's a much easier, interactive way.

Just sayin'
Avatar lilwayne1556
12 Sep 2013 03:38
In reply to NillocSkywalker
This is an advanced tutorial. It's expected you know the basics first. There are tons of Java Script tutorials but not a lot of advanced ones. You will understand this someday.
Avatar TacoRocco
12 Sep 2013 03:01
Stahp, no hablo Java!
Avatar lilwayne1556
12 Sep 2013 03:02
In reply to TacoRocco
Java Script* I no hablo Java either.
Avatar TacoRocco
12 Sep 2013 13:39
In reply to lilwayne1556
I said Java because I know that you can't say java script in chat at least.
Wii, Nintendo DS, Nintendo DSi are ™ Nintendo Co. Ltd. Opera is a product of Opera Software ASA. This website is ©2009-2018 Daniel Gump. All rights reserved. Members are responsible for their own content. No account information will be given to third-parties.