Blogger Information
Blog 28
fans 0
comment 0
visits 26725
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
创建对象的方式(字面量或构造函数)
YHF的博客
Original
1023 people have browsed it

编程: 创建对象的方式(字面量或构造函数)

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>基本语法</title>
    <script type="text/javascript">
        //创建对象方式:
        //字面量
        var obj1= {name:'YHF',age:20,salary:5000}
        //构造函数
        var obj2=new Object()
        obj2.name='YHF'
        obj2.age=20
        obj2['salary']=6000
        console.log(obj2)
        console.log(obj1)
        //new 函数名()也是可以创建的
    </script>
</head>
<body>

</body>
</html>

运行实例 »

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

1.jpg

总结:

  1. 创建Object实例:
       (1). 构造函数
        var obj = new Object()
        obj.name = 'Peter'
        obj.age = 29
       (2). 字面量
       var obj = {
           name: 'Peter',
           age: 29
       }

  2. //new 函数名()也是可以创建的

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