`
gyht0808
  • 浏览: 112211 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

javascript方法的定义

阅读更多
//定义构造函数              
function Rectangle(w,h){
    this.width = w;
    this.height= h;
}

//使用前面定义的构造函数创建一个新的Rectangle对象
var page = new Rectangle(8.5, 11);
//该函数使用了关键字this,这样它就不必自己调用自己,
//而成为了定义width属性和height属性的对象的方法               
function compute_area(a, b){
    this.width2 = a;
    this.height2 = b;
    alert("a * b = " + a * b);
    return this.width * this.height;
}
//通过把函数赋予对象的属性/来定义一个方法
page.area = compute_area;
//代码调用新方法,需要两个实际参数
var a = page.area(1, 2);
alert(a);
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics