js with用法

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

原文地址:https://www.cnblogs.com/KimhillZhang/p/2753343.html