JavaScript基础-对象

一、对象的定义

1:对象的创建(对象的实例化)

1.1:字面量创建对象

 <script>
      var obj = {//创建一个空的对象
        name: "zhang",
        age: 18,
        sex: "man",
        sayHi: function () {
          console.log("hello javascript!");
        }
      }
      obj.sayHi();//对象属性的调用  方式一
      console.log(obj['sex']);//对象属性的调用  方式二
</script>

1.2:利用new Object 创建对象

  var obj1 = new Object();
  obj1.name = "zhang";
  console.log(obj1.name);

1.3:利用构造函数创建对象

  • 定义:把对象的公共属性和方法抽取出来,然后封装到这个函数里面;类似于java中的类
  • 语法格式及要点
<script>
	  function Star(name, age, sex) {//构造函数的首字母要大写,必须使用this关键字
	    this.name = name;
	    this.age = age;
	    this.sex = sex;
	  }
	  var ldh =  new Star("刘德华", 18, "man");//必须要用 new 关键字
	  console.log(ldh['name']);//输出结果: 刘德华
</script>
<script>
  function Person(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
   //this.sayName = function () {//一般将sayName写在全局作用域中
   //   alert("我是" + this.name);
   // };
    //一般将sayName写在全局作用域中
    this.sayName = fun;
  }
  function fun() {
    alert("我的名字是" + this.name);
  }
  var obj1 = new Person("孙悟空", 18, "nan");
  var obj2 = new Person("猪八戒", 18, "nan");
  obj1.sayName();
  obj2.sayName();
</script>

1.5:注意

  • 属性值可以是任何类型,包括对象
 function Star(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  var ldh =  new Star("刘德华", 18, "man");
  var zxy =  new Star("张学友", 20, "man");
    ldh.test = zxy;
    console.log(ldh.test.name);

输出结果:
在这里插入图片描述

2:工厂方法创建对象

    <script>
      function creatPerson(name, age, gender) {
        var obj = new Object();
        obj.name = name;
        obj.age = age;
        obj.geder = gender;
        obj.sayName = function () {
          alert(name);
        }
        return obj;
      }
      var obj1 = creatPerson("孙悟空","28","男");//不同于构造函数,不需要
      var obj2 = creatPerson("猪八戒","30","男");
      obj2.sayName();
      obj1.sayName();
    </script>

3:对象的输出

  • 属性的输出
    1.方式一:obj.属性名 (属性值可以是任何类型,包括对象)
       方式二:obj['属性名’]
    方式二更为灵活,可以传入一个变量,在

  • 方法的输出
    obj.方法名();

3:对象的遍历

for( var k in obj ){ }

<script>
  function Star(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  var ldh =  new Star("刘德华", 18, "man");
  for (var k in ldh){
    console.log(k);//k变量 输出的是属性名
    console.log(ldh[k]);// obj[k] 输出的是属性值
  }
</script>

结果:
在这里插入图片描述

4:判断对象是否有该属性
“属性名” in 对象 有返回true 没有返回false

  function Star(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  var ldh =  new Star("刘德华", 18, "man");
  var zxy =  new Star("张学友", 20, "man");
    ldh.test = zxy;
    console.log(ldh.test.name);
    console.log("age" in ldh);//返回true

4:对象的栈堆内存机制

  • js中基本数据类型直接保存在栈中,值与值间不相互影响

  • new 出来的对象具有不同的内存地址。

在这里插入图片描述

二、原型对象

1:prototype

在这里插入图片描述

<script>
  function Person(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  //向Person构造函数中添加原型函数Prototype
  Person.prototype.sayName = function () {
    alert("我的名字是" + this.name);
  };
  var obj1 = new Person("孙悟空", 18, "nan");
  var obj2 = new Person("猪八戒", 18, "nan");
  obj1.sayName();
</script>

注意:prototype的属性也属于对象的属性,即公共对象区域的属性也属于自身的属性

2:hasOwnProperty

2.1: 该方法存在于propertype的属性中,使用此方法可以判断元素自身的属性,而非公共区域的属性

<script>
      function Person(name, age, sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
      }
      //向Person构造函数中添加原型函数Prototype
      Person.prototype.sayName = function () {
        alert("我的名字是" + this.name);
      };
      var obj1 = new Person("孙悟空", 18, "nan");
      var obj2 = new Person("猪八戒", 18, "nan");
      obj1.sayName();
      console.log(obj1.hasOwnProperty("sayName"));//---->输出false
      console.log(obj1.__proto__.hasOwnProperty("sayName"));//---->输出true
    </script>
</body>

三、toString()方法

1:定义

1.1:toString()方法用来修改对象的返回值

<script>
  function Person(name, age, gender) {
    this.name = name;
    this.age = age;
    this.sex = gender;
  }
  var obj1 = new Person("孙悟空", 18, "nan");
  obj1.toString = function () {
    return  "obj1[name = " + this.name + "age = " + this.age + "gender = " + this.gender + " ]";
  };
 console.log(obj1);
</script>

结果:
在这里插入图片描述

发布了23 篇原创文章 · 获赞 4 · 访问量 607

猜你喜欢

转载自blog.csdn.net/weixin_43331769/article/details/103794038