第八章 8.3 方法(Methods)

method 就是通过对象而被调用的一种 JavaScript function.  function 可以被赋值给对象的任何变量,或属性。比如:
f 是一个 function.

None.gif o.m  =  f;

一旦定义了,我们可以这样来调用:

None.gif o.m();
None.gifo.m(x, x 
+   2 );  //  如果需要参数

方法有一个非常重要的特性:用来调用方法的那个对象,在方法体内可以以 this 指针被引用。

function 和 method 虽然看上去有些不同,但本质上是没多少区别的。function 只不过是变量中保存的一些值,而变量不过是 global 对象的属性而已。所以,当你调用一个 function 的时候,本质上是调用的 global 对象的 method. 在这样的 function 里面,this 指针指代的是 global 对象。所以从技术角度来看 function 和 method 是没有区别的。
例子:
None.gif //  This function uses the this keyword, so it doesn't make sense to
None.gif//
 invoke it by itself; it needs instead to be made a method of some
None.gif//
 object that has "width" and "height" properties defined.
None.gif
function  compute_area(  )
None.gif{
None.gif    
return   this .width  *   this .height;
None.gif}
None.gif
None.gif
None.gif
//  Create a new Rectangle object, using the constructor defined earlier.
None.gif
var  page  =   new  Rectangle( 8.5 11 );
None.gif
None.gif
None.gif
//  Define a method by assigning the function to a property of the object.
None.gif
page.area  =  compute_area;
None.gif
None.gif
None.gif
//  Invoke the new method like this:
None.gif
var  a  =  page.area(  );     //  a = 8.5*11 = 93.5

上面例子的写法是不好的。因为我们必须先设定 Rectangle 的长宽属性,然后调用 area 方法才显得有意义。这可能导致错误。
下面改进后的例子说明如何在 constructor 里定义函数:

None.gif //  First, define some functions that will be used as methods.
None.gif
function  Rectangle_area(  ) {  return   this .width  *   this .height; }
None.gif
function  Rectangle_perimeter(  ) {  return   2 * this .width  +   2 * this .height; }
None.gif
function  Rectangle_set_size(w,h) {  this .width  =  w;  this .height  =  h; }
None.gif
function  Rectangle_enlarge(  ) {  this .width  *=   2 this .height  *=   2 ; }
None.gif
function  Rectangle_shrink(  ) {  this .width  /=   2 this .height  /=   2 ; }
None.gif
None.gif
None.gif
//  Then define a constructor method for our Rectangle objects.
None.gif//
 The constructor initializes properties and also assigns methods.
None.gif
function  Rectangle(w, h)
None.gif{
None.gif    
//  Initialize object properties.
None.gif
     this .width  =  w;
None.gif    
this .height  =  h;
None.gif
None.gif
None.gif    
//  Define methods for the object.
None.gif
     this .area  =  Rectangle_area;
None.gif    
this .perimeter  =  Rectangle_perimeter;
None.gif    
this .set_size  =  Rectangle_set_size;
None.gif    
this .enlarge  =  Rectangle_enlarge;
None.gif    
this .shrink  =  Rectangle_shrink;
None.gif}
None.gif
//  Now, when we create a rectangle, we can immediately invoke methods on it:
None.gif
var  r  =   new  Rectangle( 2 , 2 );
None.gif
var  a  =  r.area(  );
None.gifr.enlarge(  );
None.gif
var  p  =  r.perimeter(  );
None.gif

上面这个例子其实还是有缺点。它把五个函数都赋值给属性,而这就占据了可观的内存空间。一旦创建多个这样的对象,占用的内存会成倍增长。幸运的是,JavaScript 有一个解决方案:允许对象从一个原形对象(prototype object) 继承属性。下一节我们来详细探讨这个技术。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值