Blogger Information
Blog 36
fans 1
comment 0
visits 32445
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript 创建对象_2018年9月11日
宋超的博客
Original
553 people have browsed it

javascript创建对象实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>javascript 创建对象</title>
</head>
<body>
<script>

// 1.通过Object创建对象
   let person=new Object();
    person.name="xiaopi";
    person.age=26;
    person.sex="male";
    console.log(person.name + " is " + person.age + " years old.");

// 2.通过字面量创建对象
    let person1={name:'xiaojun',age:22,sex:'female'};

    console.log(person1);
// 2.通过构造函数创建对象
    function person2() {
        this.name='xiaohong';//通过this关键字设置默认成员
        this.age='23';
        this.sex='female';
        this.Introduce=function(){
            alert('MY name is '+this.name+' I\'m '+this.age+'years old '+this.sex);
        };
        console.log('MY name is '+this.name+' I\'m '+this.age+'years old '+this.sex);

    }
    let person3 = new person2();
    person3.Introduce();


</script>


</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:javascript中一切皆对象。

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post