Email: info@zenconix.com

Object Oriented Javascript part-2

Published on: 12/19/13 5:48 PM

Category:Javascript Tags:

Today we will see more concepts on the javascript

this article is continued article of yesterday’s one Object oriented javascript

Today we will see some more concept of object oriented javascript which are essential for writing script with OOP

First we will see To instantiate or not to be instantiate

Change in object literal will affect that object accross the entire script whereas in constructor function is instantiated and then changes is made to that instant, won’t affect any other instance

for example

[code language="javascript"]<br /><br />var myObjectLiteral={<br /><br />myProperty: 'this is a property'<br /><br />}<br />//alert this property<br />alert(myObjectLiteral.myProperty);<br />//this will alert as: 'this is a property'<br />now change this property<br />myObjectLiteral.myProperty='this is new property'<br />//alert this<br />alert(myObjectLiteral.myProperty);<br />//this will alert as: 'this is new property'  as we have expected<br />[/code]

Now create new variable and point towards the object, it will
have some changes

[code language="javascript"]<br />var myObjectLiteral = {<br />myProperty: 'this is my property'<br />}<br />alert(myObjectLiteral.myProperty);<br />// this will alert as: 'this is my property'<br />// take new variable now<br />var someObject = myObjectLiteral;<br />// Change property<br />myObjectLitral.myproperty = 'this is new property' ;<br />alert(someObject.myProperty);<br />&lt;strong&gt;//this will still alert : this is my property&lt;/strong&gt;<br />[/code]

There will not change for new variable

Now we will do same for constructor

[code language="javascript"]<br />var myObjectConstructor = function(){<br />this.myProperty = 'this is property';<br />// now instantiate constructor<br />var constOne = new myObjectConstructor();<br />// instantiate second Constructor<br />var consTwo = new myObjectConstructor();<br />// now we will alert one by one<br />alert(constOne.myProperty);<br />alert(constTwo.myProperty);<br />//this both will alert as : 'this is property'<br />}<br /><br />[/code]

so as expected this will return correct values but lets change myProperty for one of the instance

[code language="javascript"]<br />var myObjectConst = function(){<br />this.myProperty = 'this is property' ;<br />}<br />//instantiate constructor ;<br />var constOne = new myObjectConst();<br />//change in property now<br />constOne.myProperty = ' this is new ' ;<br />// instantiate second constructor<br />var constTwo = new myObjectConst();<br />//alert this<br />alert(constOne.myProperty);<br />//this will alert as 'this is new '<br />alert( constTwo.myProperty);<br />//but this will alert as: ' this is property '<br />[/code]

as you can see the differences in both of this , there is change in alerts when we change the properties of the constructor but it will not affects the previous one or entire script

Hope you understood this concept

Enjoy Javascript !!!


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Gets a username from SharePoint’s User profile service

Here is script of “How to get username from user id from Active directory”   [code language=”css”] // get user name from id function getUserFromId(userid) { // var userid = _spPageContextInfo.userId; var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")"; var requestHeaders = { "accept": "application/json;odata=verbose" }; $.ajax({ url: requestUri, async: false, contentType: "application/json;odata=verbose", […]

Get the User ID of Active Directory name in SharePoint by Javascript

How to get User Id from Site User if you have User’s active directory name [code language=”css”] //Get the id of user from AD name. function getADNameId(adName) { var UserData; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/SiteUsers?$Select=Id,Title&amp;$filter=Title eq ‘" + adName + "’", type: "GET", async: false, headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose" }, success: function (data) { […]

Get selected element Id from Task List

“Very BAD practices !!!”  Sometimes situation comes that you don’t have any other options but you have to work on it and get work done. If this situation comes then you are just searching for any possible solution (many times you know that it is wrong approch to do so…).  I have come across the situation, […]