JS对象

作者: 醉叶惜秋 | 来源:发表于2018-08-09 15:33 被阅读24次

JS 创建对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS 中创建对象</title>
    <script type="text/javascript">
        //this 所在函数属于哪个对象.他就代表哪个对象
        //创建对象
        var dog = {
            age:10,
            weight:20,
            height:1.2,
            dogFriends:['a','b','v','e','r'],
            name:'dahuang',
            //方法
            eat: function (something) {
                console.log( this.name + '吃' + something);
            },
            run:function (somewhere) {
                console.log(this.name + '在' + somewhere +'跑');
            }
        };//object

        console.log(typeof dog)
        //输出够对象的属性与行为
        console.log(dog.age, dog.eat('肉'),dog.run('操场'))

    </script>
</head>
<body>

</body>
</html>

批量创建对象

image.png
image.png

相关文章

网友评论

    本文标题:JS对象

    本文链接:https://www.haomeiwen.com/subject/ztzqbftx.html