javascript 中 with 的用法

来源:互联网 发布:sql注入漏洞的危害 编辑:程序博客网 时间:2024/05/20 06:40

举例说明

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;            }        }    }


0 0