Blogger Information
Blog 32
fans 0
comment 0
visits 24219
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js基础——对象的创建2018年9月24日19点28分
Nevermore的博客
Original
667 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>创建对象的方式</title>
</head>
<body>
<script>
    // 构造函数创建对象
    function F4(name,age,hoppy)
    {
        this.name=name;
        this.age=age;
        this.hoppy=hoppy;
    }
    let p1=new F4('大米',99,'抽烟,喝酒烫头');
    document.write(p1.age);

    // 直接new Object创建对象
    let p2=new Object();
    p2.name='what?';
    p2.age=29;
    p2.info=function () {
        return this.name+':'+this.age;
    };
    document.write(p2.age);

     // 字面量创建对象
    let p3={name:'小麦', age:26 ,info:function () {
            return this.name+':'+this.age;
        } };
    document.write(p3.info());
</script>

</body>
</html>

运行实例 »

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

Array.isArray([1,2,3])      // true

Array.isArray({x:1,y:3})    // false

在javascript中,外部声明的变量可以在函数中直接使用

var声明,块作用域外部可以拿到,let声明,块作用域外部拿不到。let不允许重复定义,var允许


对象的属性可以是简单的值,也可以是复杂的值,如函数和对象,所以访问对象的属性不仅可以用   对象.属性   ,也可以用  对象['属性名']


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