`

js使用之with的用法

 
阅读更多

with语句可以方便地用来引用某个特定对象中已有的属性,但是不可以用来给对象添加属性。如果要给对象创建新的属性,必须明确地引用该对象。

语法:with (<对象>){<语句>}

例子:

function Person(){

    this.name='xiaogang';

    this.age='15';

    this.gender='boy'

}

var person1=new Person();

with(person1){

 documnt.write(name+":"+age+":"+gender);

}

省去了多次写对象名的麻烦。。。

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics