Blogger Information
Blog 33
fans 0
comment 0
visits 24448
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript创建对象的两种方式 20180911 23:00
EmonXu的博客
Original
530 people have browsed it

JavaScript有两种创建对象的方式:字面量或构造函数。

示例代码如下:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
        // 一、构造函数创建对象
        let team1=new Object();
        team1.name='76ers';
        team1.addr='PHI';
        team1.say=function () {
            return '大家好,我们是'+this.name+'队,我们的城市是'+this.addr;
        }
        console.log(team1.say());

        //二、字面量函数创建对象
        let team2={
            name:'lakers',
            addr:'LA',
            say:function () {
                return '大家好,我们是'+this.name+'队,我们的城市是'+this.addr;
            }
        }
        console.log(team2.say());
    </script>
</head>
<body>

</body>
</html>

运行实例 »

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


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