看以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();//效果跟上面一样。
   }
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
猜你喜欢
  • 2021-11-08
  • 2021-06-21
  • 2021-09-29
  • 2021-12-25
  • 2022-12-23
相关资源
相似解决方案