美文网首页
with语句

with语句

作者: 他在发呆 | 来源:发表于2017-07-02 21:07 被阅读0次

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

    一个简单的例子

        function Lakers () {
            this.name = 'jike';
            this.age = 22;
            this.gender = "boy";
        }
        let perple = new Lakers();
        with (perple) {
            let str = "姓名:" + name + "<br/>";
            str += "年龄" + age + "<br/>";
            str += "性别" + gender;
            document.write(str)
        }
    
    
    image.png

    相关文章

      网友评论

          本文标题:with语句

          本文链接:https://www.haomeiwen.com/subject/ehsocxtx.html