M Sheik's profileudooz!PhotosBlogLists Tools Help

Blog


    May 28

    Discipline in Object Orientation – [JavaScript – The Platform for Web 2.0 Part III]

    As per our "Anything on…" slogan, there are numerous ways we can define properties and methods for a class. This flexibility is more helpful in the implementation of bulky script animation, drag n drop, etc. But for a normal UI validation and business entity manipulation we have to follow a discipline. This will enable us to maintain and enhance a JavaScript code in long-time manner.

    Firstly, let us see the different ways to define a class.

    Object Declaration

    Way 1

    See the code snippet 1.

    function Point(x, y)

    {

    this.x = x;

    this.y = y;

    this.toString = function(){return "(" + this.x + "," + this.y + ")";}

    }

    var point = new Point(10, 5);

    alert(point.toString()); //shows (10,5)

    Code Snippet 1

    Figure 1 explains the nature of this approach.

    A class named "Point" is defined with two properties (x and y) and a method "toString()". All the definitions are bundled within "Point" constructor. But technically, there are two issues with this approach; both make this approach as not optimal:

    • Each instance of Point class will have its own copy of members (x, y and 'toString()'). It is feasible for "field" declarations, but harmful for "method" declaration. Each instances has their own copy of code for "toString()".
    • The arguments of above constructor (x and y) are global to all members. Yes, toString() can access the arguments "x" and "y". Is it optimal?

    Way 2

    As we know that "new" operator in JavaScript enables us to create instance of a class. The function followed by "new" means that it acts as a constructor for the class. The declaration and initialization are happened in the same place as like code snippet 1. Also, JavaScript does the more work. The lifecycle of an object creation is depicted in figure 2.

    1. Point class is declared and defined.
    2. An object of Point is created.
    3. JavaScript, creates "prototype" object with "constructor" property refers the Point constructor and add this as a property of point instance.
    4. If we defined Point as specified in code snippet 2, all the prototype specific members are initialized and added to point instance's prototype member.

    In figure 2, upto point 3, there is no different between way 1 and way 2. After instantiating Point with "new", JavaScript add an object "prototype" to the instance "point" as property. By default, "prototype" object has a property named "constructor". Whenever, prorotype is initiated, it assigns the corresponding object's constructor function to its "constructor" property.

    If we define the Point class properties and methods in its prototype object, then it can be sharable. That is, each instance has separate "property" values and share same function point. See code snippet 2.

    Point = function(x, y)

    {

    this.x = x;

    this.y = y;

    };                 

            

    Point.prototype.toString = function()

    {

    return "(" + this.x + "," + this.y + ")";

    }

    Code Snippet 2

    We can refactor code snippet 2 to create more discipline in the next article.

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://www.nizikaikun.com/
    http://www.ms-online.co.jp/eshop/ona_hole.php
    http://www.ms-shop.co.jp/shop/goods/goods.asp?category=5308
    http://www.ms-online.co.jp/eshop/goods/vibe.php
    http://www.ms-online.co.jp/eshop/goods/costume.php
    http://www.chojudo.ne.jp/
    http://www.open-japan.com/
    http://www.open-japan.com/ideabox/index.php?category=beauty#top
    http://www.open-japan.com/ideabox/index.php?category=dress#top
    http://www.eic-av.com/
    http://www.nicolas-dogs.com/
    http://www.aqua01.net/
    http://www.kabudayo.com/
    http://www.fxf-business.com/
    http://kaketayo.sakura.ne.jp/
    http://www.11cash.net/
    http://www.saimu0.jp/
    http://telink.jp/
    http://www.complete-watch.com/
    http://www.tokei-biho.com/
    http://www.e-msqs.com/
    http://www.hattori-tax.jp/
    http://www.webtravel.co.jp/
    http://www.webtravel.co.jp/usa/index.html
    http://www.webtravel.co.jp/asia/chaina/index.html
    http://www.webtravel.co.jp/aironly/index.htm
    http://www.webtravel.co.jp/hotel/oahu/halekurani.htm
    http://www.colorcontact1.com/
    http://www.trivy-system.com/Sinyou.htm
    http://www.effect-japan.com/
    http://www.myhome-rd.co.jp/
    http://my-trunk.jp/
    http://www.kabu-net.com/
    http://www.trivy-system.com/Kekkon.htm
    http://www.kabarai-seikyu.com/
    http://www.sigmac.jp/
    http://www.e-fil.co.jp/
    http://www.sample-net.com/
    http://www.clasic.jp/goods/nudefitcoat.htm
    http://www.clasic.jp/goods/tradcasey.htm

    http://www.clasic.jp/
    http://www.bit-of-sound.com/
    http://www.hyousatu.com/
    http://www.a-world.co.jp/
    http://www.a-toy.ne.jp/
    http://www.blyjapon.com/
    http://adultshop.co.jp/omocha.html
    http://adultshop.co.jp/adultshop.html
    http://adultshop.co.jp/andropenis.html
    http://adultshop.co.jp/andropenis2.html
    http://adultshop.co.jp/onahole.html
    http://adultshop.co.jp/houkei.html
    http://adultshop.co.jp/anal.html
    http://adultshop.co.jp/enemagra.html
    http://adultshop.co.jp/denma.html
    http://adultshop.co.jp/dutch.html
    http://furniture.michiookamoto.com/
    http://www.achelabo.jp/
    http://www.takumi-pg.com/
    http://www.omochacha.com/
    http://www.av-one.jp/zero/top.html
    http://umanity.jp/
    http://www.bse.jp/
    http://www.claudia.co.jp/tuhan/goods/beauty/yomogi_hot_sheet/index.html
    http://www.claudia.co.jp/tuhan/goods/beauty/ekusute_kit/index.html
    http://yumemirai.ne.jp/
    http://ftb-rmt.jp/
    http://ftb-rmt.jp/atlantica/
    http://ftb-rmt.jp/chusen/
    http://ftb-rmt.jp/arad/
    http://ftb-rmt.jp/tw/
    http://ftb-rmt.jp/redstone/
    http://ftb-rmt.jp/nobunaga/
    http://ftb-rmt.jp/ff11/
    http://ftb-rmt.jp/lh/
    http://ftb-rmt.jp/lineage2/
    http://asaka.century21web.jp
    http://niiza.century21web.jp
    http://wako.century21web.jp
    http://www.21tokiwa.co.jp
    http://www.century21web.jp
    http://shiki.century21web.jp
    http://www.trivy-system.com/Sinyou.htm
    http://www.trivy-system.com/Kekkon.htm
    Jan. 5

    Trackbacks

    The trackback URL for this entry is:
    http://udooz.spaces.live.com/blog/cns!1EA86C1FB1C134B8!198.trak
    Weblogs that reference this entry
    • None