|
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 functionYou can define a function in two ways: function newFunction(param){ //do some stuff in function return param; }
function%20newFunction%28param%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%20in%20function%0A%20%20%20%20return%20param%3B%0A%7D var newFunction = function(param){ //do some stuff in function return param; }
var%20newFunction%20%3D%20function%28param%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%20in%20function%0A%20%20%20%20return%20param%3B%0A%7D These two are the same. And same as any other languages you can call them: newFunction('pass parameters');
newFunction%28%27pass%20parameters%27%29%3B 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. var result = newFunction(3); var instance = new newFunction(2); alert(result); //yields 3 alert(instance); //yields object
var%20result%20%3D%20newFunction%283%29%3B%0Avar%20instance%20%3D%20new%20newFunction%282%29%3B%0Aalert%28result%29%3B%20%2F%2Fyields%203%0Aalert%28instance%29%3B%20%2F%2Fyields%20object 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:var newFunction = function(param){ //do some stuff in function this.param = param // this is object property return param; } var instance = new newFunction(2); alert(instance.param); //yields 2
var%20newFunction%20%3D%20function%28param%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%20in%20function%0A%20%20%20%20this.param%20%3D%20param%20%2F%2F%20this%20is%20object%20property%0A%20%20%20%20return%20param%3B%0A%7D%0Avar%20instance%20%3D%20new%20newFunction%282%29%3B%0Aalert%28instance.param%29%3B%20%2F%2Fyields%202 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 objectYou can create object by instantiating using new keyword when calling a function or you can var newObject = new Object(); var newObject = [];
var%20newObject%20%3D%20new%20Object%28%29%3B%0Avar%20newObject%20%3D%20%5B%5D%3B 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.newObject.size = 5; var otherObject = newObject; alert(newObject.size); //yields 5 alert(otherObject.size); //yields 5 otherObject.color = 'red'; alert(newObject.color); //yields 'red' alert(otherObject.color); //yields 'red'
newObject.size%20%3D%205%3B%0Avar%20otherObject%20%3D%20newObject%3B%0Aalert%28newObject.size%29%3B%20%2F%2Fyields%205%0Aalert%28otherObject.size%29%3B%20%2F%2Fyields%205%0AotherObject.color%20%3D%20%27red%27%3B%20%0Aalert%28newObject.color%29%3B%20%2F%2Fyields%20%27red%27%0Aalert%28otherObject.color%29%3B%20%2F%2Fyields%20%27red%27 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. var instance = new newFunction(2); alert(instance.param); //yields 2 var instance2 = instance; alert(instance2.param); //yields 2
var%20instance%20%3D%20new%20newFunction%282%29%3B%0Aalert%28instance.param%29%3B%20%2F%2Fyields%202%0Avar%20instance2%20%3D%20instance%3B%0Aalert%28instance2.param%29%3B%20%2F%2Fyields%202 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. var instance = new newFunction(2); var instance2 = new newFunction(2); instance.extra = 'hi' alert(instance.extra); //yields 'hi' alert(instance2.extra); //yields undefined
var%20instance%20%3D%20new%20newFunction%282%29%3B%0Avar%20instance2%20%3D%20new%20newFunction%282%29%3B%0Ainstance.extra%20%3D%20%27hi%27%0Aalert%28instance.extra%29%3B%20%2F%2Fyields%20%27hi%27%0Aalert%28instance2.extra%29%3B%20%2F%2Fyields%20undefined How to extend objects You can extend objects in two way: - Extending just an object
You can extend object the way described above. Simply by adding the property you want to that object
var newObject = [];//create the object newObject.size = 5;
var%20newObject%20%3D%20%5B%5D%3B%2F%2Fcreate%20the%20object%0AnewObject.size%20%3D%205%3B - 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.
newFunction.prototype.extra2 = 'bye' alert(instance.extra2); //yields 'bye' alert(instance2.extra2); //yields 'bye'
newFunction.prototype.extra2%20%3D%20%27bye%27%0Aalert%28instance.extra2%29%3B%20%2F%2Fyields%20%27bye%27%0Aalert%28instance2.extra2%29%3B%20%2F%2Fyields%20%27bye%27 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: newFunction.prototype.extra2 = 'bye'; newFunction.prototype['extra2'] = 'bye 2';
newFunction.prototype.extra2%20%3D%20%27bye%27%3B%0AnewFunction.prototype%5B%27extra2%27%5D%20%3D%20%27bye%202%27%3B The second format is helpful when you want to extend them dynamically during the runtime. Such as: var newFunction = function(param){ //do some stuff in function this.param = param return param; } var instance = new newFunction(2); //this will be loaded from server at runtime var jsonDataFromServer = { referer:'otherObject', newFunctionality: function(){alert(this.referer);} } // for( var i in jsonDataFromServer) newFunction.prototype[i] = jsonDataFromServer[i]; //later you can call instance.newFunctionality(); //yield 'something'
var%20newFunction%20%3D%20function%28param%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%20in%20function%0A%20%20%20%20this.param%20%3D%20param%0A%20%20%20%20return%20param%3B%0A%7D%0Avar%20instance%20%3D%20new%20newFunction%282%29%3B%0A%2F%2Fthis%20will%20be%20loaded%20from%20server%20at%20runtime%0Avar%20jsonDataFromServer%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20referer%3A%27otherObject%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20newFunctionality%3A%20function%28%29%7Balert%28this.referer%29%3B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%2F%2F%0Afor%28%20var%20i%20in%20jsonDataFromServer%29%0A%20%20%20%20newFunction.prototype%5Bi%5D%20%3D%20jsonDataFromServer%5Bi%5D%3B%0A%2F%2Flater%20you%20can%20call%0Ainstance.newFunctionality%28%29%3B%20%2F%2Fyield%20%27something%27 What are propertiesProperties are extending our objects with attributes and functionalities. such as: // Attributes //extend prototype so all objects of this family will be affected newFunction.prototype.color = 'red'; //extend just an object (instance) instance.priority = 'important' // // Functionalities function newFunctionality(i){ //do some stuff alert(i); } newFunction.prototype.myMethod = newFunctionality //or you can write it this way newFunction.prototype.myMethod = function(i){ //do some stuff alert(i); } // you can extend objects too instance.extraMethod = function(){ //do some stuff }
%2F%2F%20Attributes%0A%2F%2Fextend%20prototype%20so%20all%20objects%20of%20this%20family%20will%20be%20affected%0AnewFunction.prototype.color%20%3D%20%27red%27%3B%0A%2F%2Fextend%20just%20an%20object%20%28instance%29%0Ainstance.priority%20%3D%20%27important%27%0A%2F%2F%0A%2F%2F%20Functionalities%0Afunction%20newFunctionality%28i%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%0A%20%20%20%20alert%28i%29%3B%0A%7D%0AnewFunction.prototype.myMethod%20%3D%20newFunctionality%0A%2F%2For%20you%20can%20write%20it%20this%20way%0AnewFunction.prototype.myMethod%20%3D%20function%28i%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%0A%20%20%20%20alert%28i%29%3B%0A%7D%0A%2F%2F%20you%20can%20extend%20objects%20too%0Ainstance.extraMethod%20%3D%20function%28%29%7B%0A%20%20%20%20%2F%2Fdo%20some%20stuff%0A%7D 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 |