<terminal>
/**
*    @Author: Arash Karimzadeh
*    @Email: me@arashkarimzadeh.com
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 227417
Bookmark and Share
JavaScript Prototype, Inheritance and Instance and Objects PDF Print E-mail
Written by Arash Karimzadeh   
Wednesday, 11 February 2009 11:56

You sure have seen many codes written in JavaScript using functions, new keyword, objects, assigning object to others, calling a function with new keyword, and adding property to functions. Wow, It is weired language comparing to others.

Let's recheck all these and discuss about them.

Here we will talk about these:

How to define a function

You can define a function in two ways:

  1. function newFunction(param){
  2. //do some stuff in function
  3. return param;
  4. }
 
  1. var newFunction = function(param){
  2. //do some stuff in function
  3. return param;
  4. }
 

These two are the same. And same as any other languages you can call them:

  1. newFunction('pass parameters');
 

How to define a class

If you use new keyword when you are calling the function, it will cause javascript to instantiate an object from that function.

  1. var result = newFunction(3);
  2. var instance = new newFunction(2);
  3. alert(result); //yields 3
  4. alert(instance); //yields object
 
So here you see when we use new keyword we are instantiating it and if we define some properties we can access them in this new object:

  1. var newFunction = function(param){
  2. //do some stuff in function
  3. this.param = param // this is object property
  4. return param;
  5. }
  6. var instance = new newFunction(2);
  7. alert(instance.param); //yields 2
 
As it seems we can use function in two ways:

  • First such as ordinary function
  • Using new keyword to act as constructor of a class

How to create an object

You can create object by instantiating using new keyword when calling a function or you can

  1. var newObject = new Object();
  2. var newObject = [];
 
These two lines are doing the same thing. You can chose one of them to create an object and sure you can add properties to it.

  1. newObject.size = 5;
  2. var otherObject = newObject;
  3. alert(newObject.size); //yields 5
  4. alert(otherObject.size); //yields 5
  5. otherObject.color = 'red';
  6. alert(newObject.color); //yields 'red'
  7. alert(otherObject.color); //yields 'red'
 
As you see object are reference type. It means when you define otherObject = newObject. It means both of them are referencing to the same object. so if you extend one of them with extra properties, the other is extending too. and they are the same object with two different names.

It is the same when you are creating the object by using new keyword.

  1. var instance = new newFunction(2);
  2. alert(instance.param); //yields 2
  3. var instance2 = instance;
  4. alert(instance2.param); //yields 2
 

But when we use new keyword each instance will be act separately. As it is shown below if you extend one instance, it will not be applied to the other one.

  1. var instance = new newFunction(2);
  2. var instance2 = new newFunction(2);
  3. instance.extra = 'hi'
  4. alert(instance.extra); //yields 'hi'
  5. alert(instance2.extra); //yields undefined
 

How to extend objects

You can extend objects in two way:

  1. Extending just an object
    You can extend object the way described above. Simply by adding the property you want to that object
    1. var newObject = [];//create the object
    2. newObject.size = 5;
     
  2. Extending family of objects
    You can extend family of objects by extending their prototype property of the constructor. Each object constructor in javascript has a property called prototype.If you extend this property with your own properties.All instances of that will inherit these new properties.
    1. newFunction.prototype.extra2 = 'bye'
    2. alert(instance.extra2); //yields 'bye'
    3. alert(instance2.extra2); //yields 'bye'
     
    As you see we extendthe constructor of instance and instance2 so now both has the new property

The best part about extending object is you can extend them when ever you want. So it means you can also extend built-in Objects such as Array, Date and ... .

How to extend objects dynamically

You can add properties in two format:

  1. newFunction.prototype.extra2 = 'bye';
  2. newFunction.prototype['extra2'] = 'bye 2';
 

The second format is helpful when you want to extend them dynamically during the runtime. Such as:

  1. var newFunction = function(param){
  2. //do some stuff in function
  3. this.param = param
  4. return param;
  5. }
  6. var instance = new newFunction(2);
  7. //this will be loaded from server at runtime
  8. var jsonDataFromServer = {
  9. referer:'otherObject',
  10. newFunctionality: function(){alert(this.referer);}
  11. }
  12. //
  13. for( var i in jsonDataFromServer)
  14. newFunction.prototype[i] = jsonDataFromServer[i];
  15. //later you can call
  16. instance.newFunctionality(); //yield 'something'
 

What are properties

Properties are extending our objects with attributes and functionalities. such as:

  1. // Attributes
  2. //extend prototype so all objects of this family will be affected
  3. newFunction.prototype.color = 'red';
  4. //extend just an object (instance)
  5. instance.priority = 'important'
  6. //
  7. // Functionalities
  8. function newFunctionality(i){
  9. //do some stuff
  10. alert(i);
  11. }
  12. newFunction.prototype.myMethod = newFunctionality
  13. //or you can write it this way
  14. newFunction.prototype.myMethod = function(i){
  15. //do some stuff
  16. alert(i);
  17. }
  18. // you can extend objects too
  19. instance.extraMethod = function(){
  20. //do some stuff
  21. }
 

As I mentioned before you can extend built-in objects such as Array() of javascript with extra functionalities.

This article originally published at my personal website www.arashkarimzadeh.com. You can read the latest version here.

Tags: JavaScript

Last Updated on Friday, 13 February 2009 07:48
 
</terminal>