javascriptのwithの使い方
1378 ワード
例をあげて説明する
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 onclick="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;
}
}
}