Blogger Information
Blog 34
fans 1
comment 0
visits 23175
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript之创建对象的方式_2018-09-11
theYon的博客
Original
465 people have browsed it

javascript之创建对象的方式

主要知识点

1)对象字面量

2)自定义构造函数

3)数组字面量

代码

对象字面量与自定义构造函数

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div>test</div>
</body>
<script>
    // 创建对象的方式(字面量或构造函数)
    // 字面量
    var cat = {
        name: '喵喵怪',
        say:function(){
            return 'cat is called '+ this.name;
        }
    };

    console.log(cat.say());

    // 构造函数
    var Person = function(name){
        this.name = name;
        this.run = function(){
            return 'i am run but '+this.name+' not';
        }
    }

    var tom = new Person('tom');
    console.log(tom.run());
</script>
</html>

运行结果

TIM截图20180913223849.png



Correction status:Uncorrected

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