javascript方法区别 本帖最后由 ZiSheng 于 2011-03-31 18:20:12 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.area是动态方法,Rectangle.prototype.area是原型方法!字面意思是这样! 实际使用中,比如var cc=new Rectangle();cc.area(),对于第一种定义会实例化很多次area函数,但是原型Rectangle.prototype.area则只实例化一次,所有对象都共享该函数,从某个角度说,第二种原型方法效率高于this.area!具体我也没有分析过,如有错误欢迎指正! 对于第一种定义,也就是说每个对象都有一个属于自己的area方法? 第一种就是每个new出来的对象都会拥有自己的area方法,第二种事对象共享的,也就是类似于java的static方法。 function Rectangle1(w,h){ this.width =w; this.height =h; this.area =function(){return this.width*this.height;}}function Rectangle2(w,h){ this.width =w; this.height =h;}Rectangle2.prototype.area = function(){return this.width*this.height;}var a1 = new Rectangle1(1, 1);var b1 = new Rectangle1(1, 1);var a2 = new Rectangle2(1, 1);var b2 = new Rectangle2(1, 1);alert([a1.area === b1.area, a2.area === b2.area]);输出:false,true即:第一种每次构造会产生新的方法,第二种构造不会有新的方法产生。结论就是当构造的对象很多时,用prototype声明的方法低碳。 请问javascript的帧是什么 iframe chrome11中如何自适应高度? jsp单元格的问题 javascript问题: 下面的代码怎么才能实现在文本域后显示出用户名不能为空~ 100进来看看. LigerUI表格插件的问题 动态生成table中的cell,使该组cell有链接的功能<a href ="#" onclick=""></a> 怎么查找字符串数组myArray里某一项包含文本“abc”呢?急。。谢谢 ******如何调用iframe页面中的一个function***** 正则表达式里减号的问题 请教一个Jquery的基础问题,高手进
this.width =w;
this.height =h;
this.area =function(){return this.width*this.height;}
}function Rectangle2(w,h){
this.width =w;
this.height =h;
}
Rectangle2.prototype.area = function(){return this.width*this.height;}var a1 = new Rectangle1(1, 1);
var b1 = new Rectangle1(1, 1);var a2 = new Rectangle2(1, 1);
var b2 = new Rectangle2(1, 1);alert([a1.area === b1.area, a2.area === b2.area]);
输出:false,true即:第一种每次构造会产生新的方法,第二种构造不会有新的方法产生。
结论就是当构造的对象很多时,用prototype声明的方法低碳。