举例说明
function students() {
this.name = 'Jerry';
this.age = 18;
this.score = function(yuwen,shuxue,waiyu) {
return yuwen + shuxue + waiyu;
}
}
stu = new students();
//一般的写法
stu.name = 'Jack';
stu.age = 28;
console.log("name : " + stu.name + ", age: " + stu.age + ", score : " + stu.score(85,90,92));
//使用with后,就相当于作用域此对象了
with(stu) {
name = 'tom';
age = 23;
console.log("name : " + name + ", age: " + age + ", score : " + score(60,80,50));
}
<form οnclick="return validForm(this);">
<input type="text" name="username" id="username"/>
<input type="submit"/>
</form>
//验证个表单
//一般写的方式
function validForm1(formObj) {
if(formObj.username.value == '') {
console.log('username can not null.');
return false;
}
}
//加入with后
function validForm(formObj) {
with(formObj) {
if(username.value == '') {
console.log('username can not null.');
return false;
}
}
}