M Sheik 的个人资料udooz!照片日志列表 工具 帮助

日志


5月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.

评论 (1)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

没有名字发表:
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
1 月 5 日

引用通告

此日志的引用通告 URL 是:
http://udooz.spaces.live.com/blog/cns!1EA86C1FB1C134B8!198.trak
引用此项的网络日志