美文网首页
JS的对象

JS的对象

作者: Z了个L | 来源:发表于2016-10-05 16:30 被阅读16次

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS的对象</title>
    <script>
        // this所在的函数在哪个对象中,this就代表这个对象
        // 对象
        var dog = {
            name: 'wangCai',
            age: 18,
            height: 166,
            dogFriends: ['laiFu', 'aHuang'],
            run: function (someWhere) {
               console.log(this.name + '在跑----' + someWhere);
            },
            eat: function (someThing) {
               console.log(this.name + '在吃----' + someThing);
            }
        };

        console.log(dog.name, dog.dogFriends);
        dog.run('杭州下沙');
        dog.eat('五花肉');

    </script>
</head>
<body>

</body>
</html>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        function Dog() {
           console.log('我是一个普通函数');
        }

        // 调用函数
//        Dog();

        // 升级 ---> 构造函数 alloc init  new
        var dog1 = new Dog();
        var dog2 = new Dog();

        console.log(dog1, dog2);

    </script>
</head>
<body>

</body>
</html>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证批量产生对象</title>
    <script>
        // 构造函数
        function Dog() {
            this.name = null;
            this.age = null;
            this.height = null;
            this.dogFriends = [];
            this.eat = function (someThing) {
                console.log(this.name + '吃' + someThing);
            };
            this.run = function (someWhere) {
                console.log(this.name + '跑' + someWhere);
            }
        }

        // 产生对象
        var dog1 = new Dog();
        var dog2 = new Dog();

        dog1.name = 'lili';
        dog1.age = 18;
        dog1.dogFriends = ['232', '007'];


        dog2.name ='wangcai';
        dog2.age = 1;
        dog2.height = 0.22;

        console.log(dog1, dog2);

        dog1.eat('骨头');

        dog2.eat('奶');


        // 带参数
        function OtherDog(name, age, height, dogFriends) {
            this.name = name;
            this.age = age;
            this.height = height;
            this.dogFriends = dogFriends;
            this.eat = function (someThing) {
                console.log(this.name + '吃' + someThing);
            };
            this.run = function (someWhere) {
                console.log(this.name + '跑' + someWhere);
            }
        }

        var dog3 = new OtherDog('w', 19, 1122, ['2121']);

        console.log(dog3);
    </script>
</head>
<body>

</body>
</html>

相关文章

  • jQuery对象和DOM对象

    1、dom对象(js对象)使用js的方式获取到的元素就是js对象。 例如: 裤子 入口文件: 1、js对象 $(f...

  • JS面向对象精要(二)_函数

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(三)_理解对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(四)_构造函数和原型对象

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • JS面向对象精要(五)_继承

    JS面向对象精要(一)_原始类型和引用类型JS面向对象精要(二)_函数JS面向对象精要(三)_理解对象JS面向对象...

  • js和jquery对象转换

    js对象转换为jquery对象 jquery对象转换为js对象

  • js 对象和jquery对象的比较

    1、js 对象和jquery对象的区别 jquery对象是js中的new Object()生成的普通对象 2、js...

  • jQuery前端框架--笔记

    1,JS对象和jQuery对象的区别 jQuery就是JS中的new Object生成的普通对象。 2,JS对象和...

  • JS 对象

    JS对象 JS对象的意义和声明 在JS中,对象(OBJECT)是JS语言的核心概念,也是最重要的数据类型。在JS中...

  • DOM对象和事件

    Json对象 json是js对象之一,是js对象的一种形式,是js对象的一个子集,可以转化成js对象,反之则不能,...

网友评论

      本文标题:JS的对象

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