现在的位置: 首页 > 综合 > 正文

js with用法

2013年04月18日 ⁄ 综合 ⁄ 共 356字 ⁄ 字号 评论关闭

看以with用法,感觉很实用,就蛮写了下;

 

with:为一个或一组 属性或方法 指定默认对象
形式:

with(对象){

   单写属性或方法

}

例:

var test = function(){
    this.str1 = "width test1"; 
    this.str2 = "width test2";//这里需使用this
};
test.prototype.method = function(){
   alert("method"); 
}

window.onload = function(){
   var t = new test();

   alert(t.str1 + " 和 " + t.str2);
   t.method();
  
   with(t){
       alert(str1 + " 和 " + str2);
       method();//效果跟上面一样。
   }
}

 

抱歉!评论已关闭.